Hello World with Java

 

Writing a Hello World program while starting to learn a programming language became a programming tradition for the past 30 years. Here is our Hello World program for introduction. Open your favorite text editor, type in the following lines and save it with the file name HelloWorld.java.

public class HelloWorld {

public static void main(String [] args) {

System.out.println(“Hello World”);

}

}

Once the file is saved, open a new terminal window, cd into the directory where you saved HelloWorld.java and compile the code the following command:

javac HelloWorld.java

The javac command is used to compile the the code in a .java file into byte code. There would be no error messages if the compilation succeeded.

At this moment, if you list the files inside the directory where you saved HelloWorld.java, you should see another file, HelloWorld.class. This file contains the byte code that was generated with the compilation and it will be used by JVM while executing the code.

java command is used to run Java byte code in a JVM. To execute the code, run the following command from the terminal window while you are inside the same directory.

java HelloWorld

The output would be

Hello World

Most developers today use an integrated development environment (IDE), like Eclipse or IntelliJ Idea during development. IDEs bring a lot of comfort while developing, like taking care of the classpath or providing real time compilation. We will talk about classpath in a later section. If you want to try out the examples with Eclipse IDE, you can follow our Eclipse tutorial.

Congratulations for your first Java program. We will go into the details about how the program works in the following section.

Anatomy of Hello World Program

In this section we will walk through our first Java program. This section is an introduction to the concepts used in Java programming and we will talk about these concepts in detail in later sections.

Every Java program starts with the main method of its class. In terms of object-oriented programming, a class is a template for initializing objects that defines the properties and behaviors of that object. Properties of the class are stored in variables and behaviors are stored in methods.

A Java class starts with the class declaration. The 1st line in the HelloWorld class declares that this is a Java class that is public. Here, public keyword declares that any class can access this class. public is an access modifier and makes its subject accessible by any class, whether the subject is a class, method or variable.

public class HelloWorld {

Following the class declaration, we declare the main method.

public static void main(String [] args) {

main() is a special method that keeps a Java code that is executed by the JVM. In other words, it is the entry point to your program and it will be run by the JVM when your class is called. It has the following signature:

public static void main(String [] args)

The contents of the main method are executed by the JVM when the class is called. The next line in HelloWorld class is the only line inside the main method.

System.out.println(“Hello World!”)

System.out.println is a method that prints the input to the console. println() method is inside System.out package and it is being called with the “Hello World” input.

Every class and method declaration in Java is followed by {, denoting the start of the class or method body and the end of the body is denoted with a }. On line 4, we declare the end of the main method’s body with a }. On line 5, the end of the class’s body is declared.

HelloWorld class is created inside the default package as we did not declare a package name in the beginning. This is not a good practice for coding Java. But before declaring our class in a package, let’s see what packages and classpath are.

%d bloggers like this: