Java Programming Tutorial Introduction to Java Programming (for Novices & First-Time Programmers)
Getting Started - Write your First Hello-world Java Program
       You should have already installed Java Development Kit (JDK). Otherwise, Read "How to Install JDK and Get Started with Java Programming".
       Let us begin by writing our first Java program that prints a message "Hello, world!" to the display console, as follows:
Hello, world!
Step 1: Write the Source Code:
Enter the following source codes using a programming text editor (such as TextPad or NotePad++ for Windows or gedit for UNIX/Linux/Mac) or an Interactive Development Environment (IDE) (such as Eclipse or NetBeans - Read the respective "How-To" article on how to install and get started with these IDEs).
Do not enter the line numbers (on the left panel), which were added to help in the explanation. Save the source file as "Hello.java". A Java source file should be saved with a file extension of ".java". The filename shall be the same as the classname - in this case "Hello".
- /*
- * First Java program, which says "Hello, world!"
- */
- public class Hello { // Save as "Hello.java"
- public static void main(String[] args) {
- System.out.println("Hello, world!"); // print message
- }
- }
Step 2: Compile the Source Code:
Compile the source code "Hello.java" into portable bytecode "Hello.class" using JDK compiler "javac". Start a CMD Shell (Windows) or Terminal (UNIX/Linux/Mac) and issue this command:
prompt> javac Hello.java
where javac is the name of JDK compiler.
There is no need to explicitly compile the source code under IDEs (such as Eclipse or NetBeans), as they perform incremental compilation implicitly.
Step 3: Run the Program:
Run the program using Java Runtime "java", by issuing this command:
prompt> java Hello Hello, world!
On IDEs (such as Eclipse or NetBeans), right-click on the source file and choose "Run As..." ⇒ "Java Application".
Brief Explanation of the Program
/* ...... */
// ... until the end of the line
These are called comments. Comments are NOT executable and are ignored by the compiler. But they provide useful explanation and documentation to your readers (and to yourself three days later). There are two kinds of comments:
- Multi-line Comment: begins with /* and ends with */, and may span more than one lines (as in Lines 1-3).
- End-of-line Comment: begins with // and lasts until the end of the current line (as in Lines 4 and 6).
public class Hello { ...... }
The basic unit of a Java program is a class. A class called "Hello" is defined via the keyword "class" in Lines 4-8. The {...} is the body of the class. The keyword public will be discussed later.
In Java, the name of the source file must be the same as the name of the public class with a mandatory file extension of ".java". Hence, this file MUST be saved as "Hello.java".
public static void main(String[] args) { ...... }
Lines 5-7 defines the so-called main() method, which is the starting point, or entry point for program execution. The {...} is the body of the method which contains programming statements.
System.out.println("Hello, world!");
In Line 6, the statement System.out.println("Hello, world!") is used to print the message string "Hello, world!" to the display console. A string is surrounded by a pair of double quotes and contain texts. The text will be printed as it is, without the double quotes.