Every Java tutorial starts with the infamous Hello world example. I would actually like to start my tutorial with a few other concepts. As the title goes, I will to describe some of the elements that make the “Hello world” example possible.
Typically, the first step in starting your journey as a Java developer is to download the JDK or Java Developer Kit. I’ve done a few sessions with people that were fairly new to Java development and when asking them to install Java on their machine there were a few who mistakenly installed the JRE or Java Runtime Environmnet.
The JRE is a set of libraries that allows you to run Java applications that have already been built and compiled to .jar files. After having installed the JRE on you machine the java executable will be available in your PATH. This allows you to run the JARs in the command line - this is true on all operating systems Windows/Mac/Linux. Openning up a terminal/command prompt you would be able to type:
and something among the line of this will show up:
java version "1.8.0_191" Java(TM) SE Runtime Environment (build 1.8.0_191-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
This confirms that java is installed on your machine. You can see the major release version - which in this case is 8 - as well as the minor one. This tells you which JVM you are running and that it’s a 64-Bit version - which is necessary to access RAM memories higher than 4GB. There is also a 32-bit version, but that is being used less and less.
You can now run Java applications. Open a terminal window or a command prompt and type:
java -jar my-application.jar
This will start running the application contained inside the JAR file.
What does the JAR file contain? Firstly JAR stands for Java ARchive. A jar file is thus an archive containing compiled java classes, metadata and resources such as text, images. A very important file within the archive is the manifest file, which contains information regarding the location of the main method which is the entry point for most applications.
The jar file is the result of the compilation process. While the compilation process isn’t very straight forward let’s just go with the fact that is that process that transforms the source code into byte code. This cannot be done using the JRE. For this we need the JDK.
The JDK provides additinal tools that only a developer would use. The most important one is the Java compiler
javac. This takes your code and transforms it into bytecode, code that can be read by the JVM. The JDK includes other tools for internationalization, security, troubleshooting and monitoring. One the tools I have personally used and I find very useful is
jvisualvm. It is a very powerful tool that provides a visual representation of various JVM metrics such as heap and CPU usage, the number of classes loaded, thread information.
You can also see information about the options passed in to the JVM.
Up until now I have mentionned a few times the acronym JVM. It stands for Java Virtual Machine. The JVM is what actually runs your code - which could Java,Scala, Kotlin, Clojure, Groovy and others. Whether you are installing the JRE or the JDK you are getting the JVM. While a discussion of what the JVM does is acutally out of the scope of this article, the key takeaway is the it is responsible for loading your classes, executing your code and managing memory. The JVM implementation is typically a piece of software written in C - but it need not be - that makes its languages run on all operating systems. I said JVM implementation, because many exist. The JVM is just a specification of what the implementation should look like.
Finally we got to the last point of this article. Java is a popular strongly-typed object-oriented programming language that runs inside the JVM. It is strongly-typed because all data needs to have a type, though in latest versions this is becomming less true. Traditionnally is has been object-oriented, but starting with Java 8 new more functional features have been added.