INTRODUCTION
In today's free Java lesson, we will analyze line by line the code that was automatically generated when we used Apache Maven to build our first Java project. We will also examine the role of the JDK in running our applications.
CODE ANALYSIS
Before we begin our analysis, let's take one more look at the code inside the class App.java.
App.java
Let's start analyzing the code line by line.
package com.example;
At the beginning of the program, we define a package to which our class App belongs to. Every class must belong to a package. The name of the package was specified when we set the group ID during the creation of the Java Maven Project.
A package is defined using the keyword package, followed by the name of the package. To complete the package declaration, we must end the statement with a semicolon (;). The semicolon is used in Java to indicate the end of a complete line of code.
It's also important to note that the package declaration must be the very first line of code in the class file. This means we first declare the package the class belongs to, and then we proceed with writing the class's code.
The name of a package can consist of one or more identifiers separated by periods (.). In our example, the package name is com.example. In reality, a package is a folder on your hard drive. Packages are used to group multiple classes that share a common functionality.
For instance, in one package, we might include only classes that handle communication and data exchange with a database. Meanwhile, in another package (within the same application), we might include classes that encapsulate the logic of our code, such as complex algorithms.
Although, from a programming perspective, we could place all the classes of an application in a single package, in practice, this would make things much more difficult, especially as the codebase grows and troubleshooting becomes necessary.
To start, and because the programs we will build (at least in the initial sections) are small and consist of only one class, we will use the com.example package. Later, when we add multiple classes to our application, we will create new packages and clearly see their usefulness.
On the very next line, we define our first class. In Java, every piece of code we write must belong to a class, even if it is just to print a simple message to the terminal.
With the keyword class, we define that the name we write immediately after (in this case, App) will be a class, and its code will be enclosed within curly brackets ({ }). The public keyword before the class indicates that other classes, from the same or a different package, can access the variables and methods defined within our class.
The keyword public defines the type of access other elements of the application have to the specific class, and it is part of a broader category called access modifiers. There are other access modifiers, which we will analyze in future sections. For now, and until we reach the theory of classes, we will use public.
The name of the class must start with a letter, and it can include numbers afterward. The naming convention suggests that the first letter of each word that makes up the class name should be uppercase. This naming style is known as PascalCase.
Another important point to know about classes is that for each class, a corresponding file is created on your hard drive with the same name and a .java extension. For the App class, in the folder named com.example, you will find the file App.java.
A class on its own is not executable, and therefore, neither is the application that contains it. In order for the class to become executable, and consequently for the entire application to run, it must contain the special main() method.
Before we proceed with the analysis of main(), it's important to highlight that Java is case-sensitive. This means that the word Main and the word main are considered two different names in Java. Therefore, we must be careful with the name of the main method to ensure our application functions correctly.
The main() method is the starting point of execution for every Java program. The keyword public at the beginning of its definition allows the Java Virtual Machine (JVM) to access it and begin execution. The keyword static enables the direct execution of the method without the need to first create an object of the class. The keyword void tells Java that this method will not return any value back to the program. In other words, the code inside the main method can display messages on the terminal, possibly as a result of some actions, but we don't expect a return value from the method itself that can be stored in a variable.
The keyword main defines the name of the method, while the parentheses () indicate its nature—it is a method. Inside the parentheses, a String array is defined in case we want to pass values to our application during its execution from the terminal. Finally, the curly braces ({ }) define the block of code that the main method will execute.
The above explanation of the code might sound difficult and unclear at first. However, as we move forward with the free Java lessons, nearly every future section will cover one of these concepts. In a short time, you will be able to easily understand all the concepts we discussed when defining the class and the main method. For now, just remember that we have a class inside a package, and we need the definition of the main method (which never changes) inside the class to run our code.
Finally, we have System, which is a static class that produces an object called out which in turn invokes a method named println(). Any text (String text) we pass as a parameter to the println() method will be displayed as output in the terminal, because it is defined as the default output for the System.out object. We always make sure to end the statement with a semicolon (;).
This simple program, in terms of execution and functionality, contains a lot of theory that we might not fully understand in depth just yet. If some (or all) of the definitions we used are not completely clear, don't worry because we will revisit them many times in the upcoming free Java lessons. For now, let's just have a quick summary:
- Java is case-sensitive.
- The simplest program requires a class.
- Each class is created within a package.
- To make the application executable, we need the main() method.
- The line that defines the main() method will remain the same for all programs.
- To display messages on the console, we use the println() method.
Let's stay a little longer on the println() method. In order for this method to display text (or String, as it's officially called in Java) on the terminal, the text must be enclosed in double quotes (" "). Also, if you write println() without passing any parameter inside the parentheses, the result that will appear in the terminal will simply be a blank line.
COMMENTS
There are also points in the code where we might want to add explanatory comments, so that when we return to the same code a few months later, they will help us quickly recall the logic of the program. Comments are a professional way to help, primarily, other developers understand the flow of the code easily.
If we want to write a comment on a single line, we use //, while for comments that span multiple lines, we can use / **/.
Comments are very useful in programming because, first, they are not printed in the final output, and second, they allow us to disable parts of the code when we need to perform troubleshooting.
To apply everything we've learned so far, write the following code, and after executing the clean and install commands in Maven, run the main() method as we previously described in the last free Java lesson.
App.java
Output
JAVA VIRTUAL MACHINE
In the first free Java lesson, we downloaded and installed the Java JDK from Oracle. Installing the Java JDK is essential for two main reasons:
- Compilation: The JDK provides the necessary tools to compile Java code into bytecode that the Java Virtual Machine (JVM) can execute.
- Running Java Applications: It includes the JVM itself, which is responsible for executing Java programs and providing the runtime environment for them.
Without the JDK, you wouldn't be able to compile or run Java applications.
The question remains—what exactly is the JVM? You’ve probably heard the reason why Java quickly became popular among developers—because programs written in Java can run on almost any operating system with little to no changes to the code. But how is this possible?
When we installed the JDK, a Java Virtual Machine (JVM) was installed on our computer. Every Java application we create and run operates on this JVM without ever "seeing" the actual operating system. In simple terms, the JVM acts as the CPU, memory, and operating system that the Java application thinks it's interacting with.
So, if you want to run your application on a Linux machine instead of Windows, all you need to do is install the JDK for Linux, and you're ready to go. The Java application will once again search for and find the JVM it needs to execute, without ever realizing that the operating system has changed.
This abstraction layer provided by the JVM makes Java applications highly portable and allows them to run across different platforms seamlessly.
Let’s go back to the last program we created. Our class, in which our code is written, has a .java extension. All the java files, which make up our application, have a .java extension and constitute the Java Source Code. So, writing and developing the Java code is the first step.
When we execute the clean and install command, what actually happens is that the JDK checks if our code is syntactically correct. If there are no errors, it then proceeds to the compile phase, where new files are created with the same name but with a .class extension. If we had an application with three classes, after the clean and install, we would expect to find three additional files with a .class extension. The .class files are created inside the target folder, which is automatically generated by Maven.
These new files contain our code in digital bytecode form. From here on, the JVM takes over, which has the ability to read these files and convert them into machine language for the corresponding operating system on which it is running.
Every time something changes in the code and we run the Maven command mvn clean install
, Maven cleans up the old .class files and creates new ones by repeating the compilation process. Additionally, a new file is created with a .jar extension, which contains all the classes of our application and is executable.
This is our entire application packaged under one name with a .jar extension, containing all the classes needed to run our application. Although the example is somewhat off the mark, just to help you understand what we've created, think of it as something similar to an .exe file in Windows. So, do we now have an executable application? And if so, if we run this .jar file on another computer where the JDK is already installed, how can we execute it? The answer is very simple: we write the Java command, then specify the path where the .class files are located, and finally the class containing the main.
Before we close this free Java lesson, let’s do a quick recap of what we have covered in theory. So, every time you run the mvn clean install
command, a .jar file is actually created, which represents the executable version of the application. Inside this .jar file (you can unzip it and see what it contains), all the classes of our application are included in bytecode form with a .class extension. These bytecode files are read by the Java Interpreter (the JVM, in other words) and are converted into machine language for the operating system on which our application is running.
Don't forget to make a small donation so that this blog can grow even more and have more capabilities in delivering free online lessons.
full-width
1 Comments
Εξαιρετικό μάθημα. Νομίζω το καλύτερο που ανέβηκε ποτέ σε ελληνόγλωσσο site!
ReplyDeleteWhat do you think about Ground of Code?