2.2. Hello World program
Last updated: 27 January 2013.
This tutorial will show you how to write, compile and run a basic program that prints "Hello World !" to the standard output. The standard output is the command prompt (for Windows users) or terminal (for Linux and Mac users). Just follow these 5 steps:
1. Open your favorite text editor and type in or copy/paste the class HelloWorld shown below. Any text editor will do the job:
- Notepad , Notepad++,... for Windows users.
- Gedit, Geany, Emacs,... for Linux users.
- TextEdit, Tincta,... for Mac users.
Java is case sensitive. Consequently, the program below must be written exactly as is.
class HelloWorld {
// My first Java program
public static void main(String[] args) {
System.out.println("Hello World !");
}
}
// My first Java program
public static void main(String[] args) {
System.out.println("Hello World !");
}
}
2. Save the file as HelloWorld.java in the directory of your choice. Don't save it as helloworld.java because the file must have the same exact name as the class that it contains.
3. If you are using Linux or Mac, open a terminal and change to the directory where the file HelloWorld.java resides. Likewise, if you are using Windows, open a command prompt and change to the directory where the file HelloWorld.java resides.
4. Compile the program by typing javac HelloWorld.java at the command prompt or terminal. If there are no compilation errors, the prompt does not print anything and a file named HelloWorld.class is generated in the same directory where the file HelloWorld.java resides. If there are compilation errors, make sure that you wrote the program exactly as above. Java being case sensitive, if you type system instead of System, your code will not compile.
5. Run the HelloWorld.class file by typing java HelloWorld (without the ".class" extension) at the command prompt or terminal. The standard output shows:
Hello World !
Code explanation
- The code above declares a class named HelloWorld. According to the Java code conventions, a class name must start with a capital letter. Furthermore, you should avoid using hyphens or underscores as separators. For instance, class names like Hello-world or Hello_world are not recommended. Instead, you should use capital letters as separators and mix them with small letters as in the class name HelloWorld. The Java code conventions are meant to make your code easily understandable by other Java developers and thus easily maintainable.
- The class content is between two curly braces:
class HelloWorld {
}
- The line starting with // is a comment. Everything that follows the double slashes on the same line is ignored by the compiler. You can also write a comment on multiple lines this way:
/*
This is a line comment
This is another line comment
*/
Note that you can also use that type of comment on a single line: /* This is a comment */. Comments can be placed anywhere. - The class contains a single method: the main method.
public static void main(String[] args) {
System.out.println("Hello World !");
}
Every Java application has a single main method which is its entry point. The main method is where the execution of a Java application starts. Its signature is always the same because the JVM looks for that signature when it starts a Java application:
public static void main(String[] args)
In the signature shown above, the String array argument of the main method contains the command line arguments (String[] args). In the next tutorials, you will learn what command line arguments are and how to use them.
- The statements of the main method are within two curly braces:
public static void main(String[] args) {
}
- There is only one statement in this case and, like all Java statements, it ends with a semicolon:
System.out.println("Hello World !");
- System.out.println is a standard way of printing text to the standard output. The text that is to be printed is between quotation marks: "Hello World !".
You are here :
JavaPerspective.com >
Beginner Tutorials >
2. Getting Started >
2.2. Hello World program
Next tutorial : JavaPerspective.com > Beginner Tutorials > 3. Java Basics
Next tutorial : JavaPerspective.com > Beginner Tutorials > 3. Java Basics