In the lesson, we will look at the Java program basic structure we coded in the previous lesson. We will inspect the various components of our “Java Hello World” program and try to understand it more closely. Here is the program we coded in the last section for our reference:
package com.javdevjoural;
public class Main {
public static void main(String[] args) {
//print Hello World!!
System.out.println("Hello World");
}
}
The first line in our program package com.javdevjoural.java.tutorial.gettingstarted;
is known as java package. This line tells the compiler that our Main class belongs to the com.javdevjoural.java.tutorial
package. A package in a Java program is a grouping of related classes and interfaces. It’s a namespace similar to the different folders on your computer where you use them to manage your files.
Don’t worry about the classes or interfaces. We will cover them in more details in the later lessons.
When we add the package
statement to our program, we are essentially asking the compiler to include this file in the com.javdevjoural.java.tutorial.gettingstarted
Package. Here are some important things to keep in mind:
.
will create a new folder).<code>gettingstarted
folder.If we navigate to the project in our computer, you will see the folder structure being created based on the package structure. This is how the package structure look like in my computer:
We got an understanding of the package in our java program basic structure. There are a few things which you should always keep in mind.
gettingstarted
and gettingStarted
are not the same thing.Java package is a name space or just folder structure, than why do we need package in java? or what are the benefits of using a well-defined package name in Java programming? Well, there are many benefits of declaring package name in your Java class.
addition package
which helping to build addition feature for my calculator.java.time
package and all classes related to time and date are part of this package.Let’s move to the second important part of our Java program basic structure, it’s out HelloWorld
class.We will talk more about classes in the later chapter, however in our example the HelloWorld
class start at line 3 with an opening curly brace {
and end with the matching closing curly brace }
. Remember that these curly braces are used a lot in the java programming to show
public class HelloWorld {
Our HelloWorld
class contains a main()
method which is the entry point for JVM or simply the Java program always starts from the main()
method.
public static void main(String[] args) {
//print Hello World!!
System.out.println("Hello World");
}
The main()
method is the entry point for any Java application. Whenever JVM runs an application, the main()
method is the first method being called. Without main() method, we can’t execute our Java program. There are a few important points for the main method:
main()
method takes an array of String as input (see the String[] args
in the main method)Our example main() method is very simple, it’s contains only 2 lines with the first line as Java comment and the second line System.out.println("Hello World");
display the “Hello World” in the output.
;
), if you don’t add the semi-colon, you might see compile time error. It’s very similar to C++ or C language.The last interesting part of the Java Program is the comment. They are used to providing more details or context to other developer. They are not used by compiler and are ignored during the compile time. Here is the Java comment example from our program.
//print Hello World!!
In the lesson, we learned about the Java Program Basic Structure and its different building blocks. We will cover these section in more details during the course but this gives us good head start. As Always the entire source code for this series is available on out GitHub Repository.