Free Java Tutorials >> Table of contents >> Hello World

1. Hello World

Your first Java program

Each program in Java starts with a file. The file extension usually ends with .java, and this is an example hello world program:

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

If you are using our code editor, you can copy this code and click on the "run" button below. However, if you are coding on your computer you may need to:

  1. Create a MyProgram.java file (the "MyProgram" must be the same name as the "public class MyProgram"). If you are using an integrated development environment (IDE) such as Eclipse / Netbeans, you may also need to create a folder (called a Java "Project" in some IDEs)
  2. Compile the program (in some IDEs this is done automatically for you). This converts the text .java file into something that can run ("execute"). If you are programming on the command line, you run javac MyProgram.java to compile your program.
  3. Run the program by clicking on the run button. If you are programming on the command line, then you will type java MyProgram to execute. Notice you do not need to type the extension ".java" when executing. This is because java executes the MyProgram.class file, which is the compiled binary.
Try running or changing the program here:

2. Comments and Errors

Comments

Every single character in the previous example was an important part of the program. However, you can add text that does not run in two ways:

//This is a single line comment. 
public class MyProgram { //The comment starts after two slashes
	/**
	 * This is a multi-line comment. The slash star begins the comment
	 * The comment ends with a star slash.
	 *
	 * Sometimes people write comments with many stars in them
	 * Only the slash star at the beginning and the star slash
	 * at the end affect where the comment starts and ends.
	 * It is important not to have a space between the / and the *
	 */
	
    public static void main(String[] args) {
        System.out.println("Hello World!"); //Outputs "Hello World"
		System.out.println("Hello Moon");   //Outputs "Hello Moon"
		System.out.println("Hello Mars");   //Notice that lines run sequentially
    }
}

In most code editors, you can automatically toggle commenting of the selected line(s) by holding the control key and the forward slash "/" key.

Errors

There are three kinds of errors that can occur in your program:

  1. Compilation errors occur when the computer cannot understand your application, and Java cannot compile the text into a runnable executable. The program will not start until you fix the "syntax error". Java has syntax and grammar similar to the C and C++ programming languages. Unlike humans, computers will often fail at understanding a program even if one tiny syntax error is present.
  2. Runtime errors occur when the program starts, but hits a problem during execution. For example, you may try to divide a number by zero, open a file that does not exist, or use too much memory.
  3. Logic errors occur when the program runs successfully, but it does not perform what the programmer wanted it to. These are some of the hardest errors ("bugs") to fix.

Understanding the hello world program

Your first Java program executes one statement of code at a time. A statement is usually a line of code that ends in a semicolon ";", but we will see that not all statements are like this.

What does public class Main and public static void main(String[] args) mean?

Java is an Object Oriented Language (OOP). Unfortunately, we will not fully understand these lines until much later in the book. For now, you should know that:

  • The file name must be the same as the public class
  • The public static void main(String[] args){ is always the starting point of your program

If you have programmed before, you should know that this class merely serves as the container for the main method. The public means that the method can be executed from any class. The static means that the method can be executed without an associate instance class object. The void means the method does not return any value. main is the method name, which cannot be changed for a starting function. The String[] args are the command line parameters that are passed into the application when it starts

Try adding comments and see that the program output doesn't change. Also try removing the semicolon to see a compilation error:

3. System.out.print

println vs print

System.out.println("Hello World"); outputs "Hello World" and a new line. Alternatively, you can do System.out.print("Hello "); followed by a System.out.println("World"); for the same effect.

Advanced programmers should note that System is a class, and out is a static variable in that class. Both print and println are methods that take things such as text (Strings), and display them via standard out to the console. This output can be redirected using IO pipes to files or other applications

Try mixing and matching print and println:

Next: Expressions