The First Java Program: Hello World

Programming is to design and implement solutions using languages understandable by computers. A programming language is a way of communication between humans and computers. Java is one of the most popular programming languages.

The goal of programming is to solve problems. Generally, problem solving involves the following steps:

  • Understanding the problem and the requirements.
  • Designing a solution (idea).
  • Implementing the solution as a program.
  • Testing the program.

The First Java Program

The following is the source code of a simple program “HelloWorld.java”, which prints out “Hello World” on screen.

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

This program defines a class with name “HelloWorld”. A class is a collection of data and functions. Classes are basic components in Java programs; each Java program is a collection of classes.

Inside the class, a method named “main” is defined. A method is a collection of programming statements that will be run when the method is called (invoked). The “main” method is a special method; when a program runs, it starts from the “main” method. The “main” method takes a parameter “args” of type “String[]”, which means an array of character strings; however, this parameter is not used in this example.

Inside the “main” method, we have only one statement, which calls another method “println” to print out a string “Hello World”. The “println” method is part of the “System.out” object, which is defined in the Java standard library.

For now, just think of this piece of code as a template for a simple Java program, and we will gradually learn about:

  • Writing statements in the “main” method to manipulate data and control flows;
  • Writing methods to abstract out common functionalities;
  • Writing classes to model real-world design requirements.

To run this program, simply do the following steps:

  • Save the code in a file with name “HelloWorld.java”. Note that, the file name must be exactly the same as the class name (with the same upper/lower case).
  • In the command-line terminal, run the following command to compile the code. This will generate a file “HelloWorld.class” containing java bytecodes, which are machine-readable instructions.
    $javac HelloWorld.java
    
  • Type in the following command to run the program.
    $java HelloWorld
    
  • The output will simply be:

    Hello World
    

Java Development Environment

JRE (Java Runtime Environment) contains tools required to run Java applications. JDK (Java Development Kit) includes JRE plus tools for developing, debugging, and monitoring Java applications. As Java developers, we need JDK installed in our system. The latest JDK download page is as below:
http://www.oracle.com/technetwork/java/javase/downloads/index.html

An integrated development environment (IDE) is a tool to facilitate programmers for software development. An IDE usually includes a source code editor, a debugger, and automated building tools. There are many powerful and free Java IDEs available.

  • Eclipse IDE for Java is the most recommended IDE for both beginners and professionals.
  • Netbeans IDE is also widely used by professional Java developers.
  • BlueJ is a very easy-to-use IDE, especially for beginners and teaching purpose.

Comments

comments