This domain is available for sale. Contact info2crazzybasket@gmail.com

Understand Java Program


In the previous chapter, we have learned how to set up the basic infrastructure to start with our first job program. Before starting with the Java code, let's create a Java project in the workspace we have created in the previous section.

New Java Project

Open file >> new >> project and look for the Java project option.

Java Project

Click on next. In this window, we will provide the basic information that is required for the project. Let's put the project name as "java-learning". You can see the location where this project is going to be saved, and an option to change it. We will keep it as it is.

The next option is to configure the JRE. You can see a list of JREs provided by eclipse and an option to use the default one. We will use the default one that we configured in the previous section

We will keep all other configurations unchanged.

Java Project

Click on next. In the new window, you can see more configurations related to the source directory and supporting Library files. We will keep all these configurations unchanged and click on finish.

The concept of package

A package is a group of related types. By type, we mean a Java class or an interface or an enumeration or an annotation. Packages help to control the access and help in avoiding duplicate names. Most of the fundamental classes in Java are part of packages like java.lang and java.io.

It is a good practice to keep all the Java classes part of some packages. Right-click the src folder, select new >> package and give a package name like com.basic

Java Package

This will automatically generate those directories com and basic inside the workspace as sub-directories.

HelloWorld.java

Let's create the first Java file. Right-click the package we have created just now. Select new >> Class. Type the class name as HelloWorld. As a best practice, all class names should be in camelcase and it should not start with any numbers. You can see that a file with the same name and extension of .java has been created.

Here is the code for your first Java program.

package com.basic;

public class HelloWorld {
//This is a method that will print "Hello World".
public static void main(String[ ] args) {
  System.out.println("Hello World");
 }
}

Let's try to understand this program line by line.

package com.basic;

The keyword package is used to represent the name of the package where the current Java class is available. Other Java classes can access HelloWorld by calling com.basic.HelloWorld.

public class HelloWorld

In Java, we use the keyword class to represent the given type as a class. The keyword public is an access modifier. Access modifiers control who can access the given class. In our case the class is public, means anyone can access HellowWorld. The statement ends with curly braces ({...}), the Java statements in between these two curly braces make the body of the class.

//This is a method that will print "Hello World".

This statement is a comment. Comments won't be executed. The compiler will simply ignore it. Comments are useful information about the code. In Java, a single-line comment can be represented like this. Multi-line comments can be represented like below.

/*
This is a
multi-line comment
in Java.
*/

public static void main(String[ ] args)

This is a Java method with the name main. Every Java method should contain an access specifier, a return type, and an optional list of arguments.

Here the access specifier of the main method is public. This method returns nothing, so we used the return type void.

This method has only one argument (args) and its return type is a String array. [] is used to represent an array.

The statement ends with curly braces ({...}), the Java statements in between these two curly braces make the body of the method.

System.out.println("Hello World");

This is the one and only one statement of the method main().
System is a built-in class available with Java. out is one of its properties of type PrintStream. println() is another method defined inside PrintStream. This method can print its argument to the console. You can see that the statement ends with a semi-colon(;). Semi-colon represents the end of that particular statement.

Running a Java Program

To run this program, simply right-click the HellowWorld.java and select Run As >> Java Application. You can see the message "Hello World" in the standard console.

Running a Java Application