Write & really understand a simple Java Program

In this article we will introduce Java through the classic Hello World application.

1. The Hello World Application

Let’s write the classic Hello World application in Java –

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

Nothing fancy. This is one of the first programs that all of us Java developers will most likely write pretty early on in our Java journey.

As new or in some cases even experienced Java developers, do we really understand everything there is to understand about this application?

Let’s look at this seemingly simple application and try to *really* understand it

2. Compile and Run Hello World application

2.1 Compiling HelloWorld.java

  1. Copy the code from the section above into a file (using notepad or any such text editor) and save it as HelloWorld.java. Note the directory where you saved this file. Let’s call it “my-directory”
  2. Next, open command shell. If you are using Windows, Start menu -> Run -> cmd.exe
  3. Navigate to “my-directory” where you saved HelloWorld.java. If you are on Windows, you can do this by running the following command in your command shell: cd path/to/my-directory (Replace path/to/my-directory with the actual path)
  4. Compile

Compilation is the step in which we covert the program that we have written into a language (instruction set) that the “Java interpreter” program can understand

In the command shell, type javac HelloWorld.java and press Enter

The javac command will compile the Java source file HelloWorld.java into java byte code and save it into a file called HelloWorld.class. Hit the enter key and you should shortly see the command completing and returning you back to the prompt.

That means the javac compiler successfully compiled our source code into byte code.

What is Java byte code? Java byte code is a collection of instructions that the Java interpreter will execute to run the application.

2.2 Run the HelloWorld Application

The Java interpreter command is java.

Lets run java on the code we just compiled. Type java HelloWorld on the command prompt.

Hit the enter key and you will see the following:

>Hello World!
>

Notice that we did not add the .class extension to HelloWorld when we invoked the interpreter. The interpreter automatically looks for a file called HelloWorld.class.

At this point, we have successfully written, compiled and run the simplest of the Java programs.

This seemingly simple program can be used to understand a whole lot of Java 🙂

3. The Java Language is Object Oriented

Java is an Objected Oriented language. Broadly speaking, that means the code is encapsulated into chunks called classes. The basic concepts to understand here are, Classes, Objects, Variables and Methods. Let’s look at each one of them briefly

Classes

A class defines what a particular chunk of code can do in methods and defines what information it keeps track of in variables. Classes can use or “leverage” other classes. Object oriented principles talk about various common strategies to write and leverage Classes. To write a Java program ,at its core, is the exercise of writing one or typically more than one Class.

Java has a large collection of predefined classes collectively known as the Standard Class Libraries. Our HelloWorld class makes use of three of these: String, System and PrintStream.

Objects

An object is a concrete instantiation of a class. Objects are spawned by the Java interpreter as it executes the byte code. When writing Java code, there are various ways to instruct the creation of an Object. One of the common ways is the use of the “new” keyword. Keywords are discussed later in this article.

An instance of the String class can be initialized by enclosing text in double quotes. For example, in our program – “Hello World!”. We will discuss objects and classes in a subsequent article.

Variables

A variable of a class is some piece of data the class manages.

Our HelloWorld class has no variables. However, the System has a variable called out. This variable is a instance of the PrintStream class. We access this variable in our HelloWorld class as System.out.

Methods

Methods perform some computation or task. Methods can take zero or more arguments and return at most a single value.

Arguments are specified by a comment separated list of the data type of the argument and the name of the argument. Methods which return a value specify the data type of the return value immediately preceding the name of the method. Methods which do not return a value use the void keyword instead.

Our HelloWorld class has a single method called main which takes a single argument of type String[] and returns no values.

The PrintStream class has a number of methods. The one we use in our class is called println which takes a single argument of type String. When we call that method it will print the provided string to the console. We access the println method of the System.out PrintStream as System.out.println.

4. Reserved Keywords

Java language has many reserved keywords. They are called reserved because each keyword has a special meaning for the Java compiler and the interpreter.

Keywords therefore, cannot be used as variable names or class names by developers.

We are making use of four reserved keywords in our application.

class

This keyword indicates to the javac compiler that we are defining a new class.

public

This keyword is a example of what is called a modifier. In our case, it is telling the compiler that both our class and the method main have public scope. We will discuss scope in more detail in a subsequent article.

static

This keyword is another example of a modifier. In our case, it is telling the compiler that our method main is a class method as opposed to an instance method. Static method means that the method does not depend on an instantiation of the class. We will discuss static in more detail in a subsequent article.

void

This keyword indicates that a method does not return a value at the end of its computation. Since our main method does not return a value, we need to indicate this with the void key word.

5. Data Structures

Data structures contain data and methods to access and possibly modify that data. Data structures are general programming concepts and have implementations in most, if not all, programming languages.

Core Java Library contains the implementation of a number of common data structures

arrays

An array is perhaps the simplest data structure. An array of a particular data type is an ordered list of instances of that type. It is indicated by []. Notice that our main method has an argument String[] args.

Related reading – Introduction to Data Structures in Java

6. Syntax

Code blocks

Code in Java is grouped together in blocks beginning with an open curly bracket { and ending with a closed curly bracket }. These are called code blocks.

All of the variable declarations and methods that define a class are grouped in such a block. This block is called the class body of the class. Likewise, all statements within a method are written in a block. This block is called the method body of the method.

Code blocks can be nested. i.e. a Code block can contain other code blocks.

Our class has a single method:

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

This method is located within the _HelloWorld_ class’s body.

Statements

A statement is an executable line of code. In Java, statements end with a semicolon ;. Our class has a single statement:

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

This statement is located within the main method’s body.

7. Java Applications

Every java application must have a method called main which takes a String array as an argument. The java interpreter will call this method when the application is launched.

Since our HelloWorld class defines this method, it will called when the interpreter is run.

8. HelloWorld, again

Now that we understand some basic concepts of the Java programming language we can examine the source code of our HelloWorld class.

We defined the class as a public class named HelloWorld. We defined a single static method which returns no values and takes a String array as an argument. This method satisfies the requirement to make our HelloWorld class a java application.

The body of this method calls the method println of the PrintStream instance defined by System class’s out variable. This method in turn prints the String we provided as an argument to the console. The String we provided was “Hello World!” which is exactly what was printed out.

If you have read so far, you’ve learned how to compile and execute a simple Java application. You were also (re)introduced some key concepts and the basic syntax of the Java programming language.