JavaDevNotes.com

Java Tutorial - Simple Hello World Java Application

A good way of learning a new language is to analyze a simple application that outputs something to the screen. This tutorial will walk you through a simple Hello World application to help you start your journey with Java.

If your have not prepared your machine yet for Java development, I recommend that you go through the Setup Your Windows Development Environment tutorial. It is much easier to learn Java if you can write/run programs and do some experimentation.

The Sample Application

Here is the complete code for the sample application:

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

When you run the application, it will display this output:

Hello World

Class Declaration

The most common code you will write in Java are classes. Detailed explanation about Java Class will be covered in succeeding Object Oriented tutorials. Only simple concerns will be covered now.

Here is the class declaration:

public class Sample {
}

  • class - the class keyword after the word public declares that Sample is a class
  • public - the public identifier tells us that this class can be invoked from the outside world. Since we want to run the program (invoked externally) the class should be public
  • File name - since we named the class Sample, it should be saved in a file with file name Sample.java. Note that Java is case sensitive. Which means that Sample and sample are different from each other
  • { } - the curly braces { and } denotes that the details of the class are contained inside these

Method Declaration

Similar to many common programming languages, Java is a functional language. You can write procedures and functions (also called methods) in Java. In our application, the class Sample contains one method named main.
public static void main(String[] args) {
}
  • main - the exact method declaration public static void main(String[] args) is special. When a class have this, it can be run as a standalone application. If you change something from it (E.g. rename main to foo, or change public to private), the class will not be able to be run as a standalone application.
  • public - the method should be declared as public to be visible from the outside world.
  • static - the keyword static means the method is not associated with any class instance. It is okay not to understand this for now. The topic will be explained in future tutorials.
  • parameters - the code portion String[] args means the method accepts an array of string as parameter.
  • { } - the curly braces { and } denotes the body of the method are inside these

Display/Print on the screen

System.out.printlny is a utility to display something on the screen. The code below displays the String "Hello World" to the screen.
System.out.println("Hello World");

Notes

This tutorial focuses on explaining a very simple Java application. Succeeding tutorials will focus on adding other Java knowledge slowly.

Exercises

What is the output of the following programs:


  1. public class ExerciseA {
      public static void main(String[] args) {
        System.out.println("This is a test message");
      }
    }

  2. public class ExerciseB {
      static void main(String[] args) {
        System.out.println("This is a test message");
      }
    }

  3. public class ExerciseC {
      public static void main(String[] args) {
        System.out.println("This is a test message");
        System.out.println("This is another test message");
      }
    }

  4. public class ExerciseD {
      public static void MAIN(String[] args) {
        System.out.println("This is a test message");
      }
    }

  5.  public class ExerciseE {
      public void main(String[] args) {
        System.out.println("This is a test message");
      }
    }
Answers

  1. The output will be like this:
    This is a test message
  2. The program will not run because main is not declared as public

  3. The output will be like this:
    This is a test message
    This is another test message
    System.out.println displays a carriage return at the end, making the second message displayed on a new line.
  4. The program will not run because the method is named MAIN and not main. Remember that Java is case sensitive
  5. The program will not run because main is not declared static.
Tags: beginner tutorial, console