This tutorial will describe in great detail how you can build a “Hello World” program in Java on a Windows (and in this case) machine. In case you are an absolute beginner user of Java, a “Hello World” program is a program used to literally display the text, “Hello World” on the screen. By creating such a program a Java programmer can test whether or not their Java installation is installed and functioning correctly and a Java beginner can gain a firm grasp of Java command-line I/O (Input/Output).

 

The block of text below is what is and will be referred to hereafter as, “Code.” The term Code is used to describe a file containing text that is typed in a particular way, using particular keywords, written under specific syntaxes in order to allow a computer to read it and act upon what it reads. The code is one of several ways that a human can “speak” to a computer. More on this at the end of this article.

package helloworld;

/*
* @author Dillon Chaffey
* Website: https://chafflube.wordpress.com
* E-mail: chafflube@hotmail.com
*/

public class HelloWorld {
   
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

 

In the block of code above there is a very short set of instructions. These instructions simply tell the computer, “Hey, could you please write the text, “Hello World!” on the computer user’s screen?” So that you can better understand how this code works, let us learn it a bit at a time.

 

package helloworld;

 

The above line of code tells the computer which “package” this .java code file is. In nearly all cases one program will be comprised of one or more .java files within the same package.

 

/*
* @author Dillon Chaffey
* Website: https://chafflube.wordpress.com
* E-mail: chafflube@hotmail.com
*/

 

The above block of code contains details of the author of the code and does not at all affect the way in which the program runs. This is due to the fact that the block of code is a comment. A comment in Java (and most other programming languages) is used merely to leave notes for anybody editing the code or to temporarily remove code from a program without actually removing it. Comments are very useful, as you will find when you begin to build larger programs.

 

public class HelloWorld {

 

The above line of code is the beginning of what is called a “class definition.” A class definition does what its name suggests, defines a class. A class in Java is a block of code that contains one or more “methods.” A method in Java is a block of code that “does something.” In the case of the line of code above, a “public” class with the name “HelloWorld” is created. Public meaning that it can be accessed from any .java file within the same package. The “{“ after “HelloWorld” is an indication that a method is about to be defined.

 

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }

 

The above block of code is what is called the “main method.” The main method is a special method in that it is always the first method to have its code run unless the programmer specifies otherwise. The first line is used to create a public, static, void method with the name “main,” which returns a string array named “args”. Similarly to defining a class, defining a method also requires that the programmer inserts a “{“ after his declaration of the method. The second line contains the actual code that causes the computer to print the text “Hello World!” Finally the “{“ on the final line indicates the end of the main method. Confusing? Don’t worry, it will all make sense if you keep tinkering with the language.

 

}

 

Finally the closing “}” indicates that the end of the class has been reached. At this point the computer will exit the program as it no longer has any code to execute.

 

Although it has been said earlier in this article that the code is read by the computer and the computer acts upon what the code contains, the computer does not read the code that we write directly. Why? Because all code must first be “compiled.” Compiling code is described in more detail below.

 

Compiling Java code

When Java code is compiled it is converted from a code that we can understand to a code that the computer can understand. In the case of Java, the source we type is stored in files with a .java extension and when the code is compiled, the compiled code is stored in a file with a .class extension.

 

When code is compiled into .class files the computer user is able to run the code by using their JVM (Java Virtual Machine). The Java Virtual Machine is used to turn our .class files into “binary” code. Binary code is the language of computers and is often seen in crime scene investigation movies. Binary code can be easily distinguished by chains of “1” and “0.” Like below:

11010010

 

The binary code is then read by the computer what it has to do. In the case of this program, print the text “Hello World!” on the computer user’s screen.

 

I hope that you have benefited from this Java tutorial and have become more interested in learning the Java language. Also, keep in mind, learning Java takes time, but it is a very rewarding experience. Good luck and stay tuned to Chafflube’s Tech Blog for more Java code tutorials.

Advertisements