Hello World Program
Here’s the simple Java program you can try:
HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println(“Hello, Java!”);
}
}
To Compile and Run (Using Terminal):
Save as HelloWorld.java
Open terminal or CMD and navigate to the file’s folder
Compile:
javac HelloWorld.java
Run:
java HelloWorld
Output:
Hello, Java!
Understanding the “Hello World” Java Program
When you begin learning any programming language, the first program you usually write is a Hello World program. It’s a simple example that introduces you to the basic structure and syntax of the language. Let’s go through a classic Java Hello World program step by step.
1. public class HelloWorld
- class: In Java, everything revolves around classes. A class is like a blueprint for objects, and every piece of code must be inside a class.
- HelloWorld: This is the class name. By convention, class names in Java start with a capital letter.
- public: This is an access modifier. It means the class is accessible from anywhere in the program or outside it.
Note: The file name must match the class name.
So, if your class is named HelloWorld, the file should be saved as HelloWorld.java.
2. public static void main(String[] args)
This line is the entry point of every Java program. Let’s break down each part:
- public: Means the method can be called from outside the class. The Java Virtual Machine (JVM) needs to access it, so it must be public.
- static: Means this method belongs to the class, not an object. This way, the JVM can run the method without creating an object of the class.
- void: This is the return type. void means the method does not return any value.
- main: The name of the method. The JVM looks specifically for main as the starting point of a Java application.
- String[] args: This is an array of Strings that can store command-line arguments. For example, if you run the program with:
java HelloWorld Welcome
Then args[0] will contain “Welcome”.
Note: Without the main method, the program won’t run because the JVM won’t know where to start.
3. System.out.println(“Hello, Java!”);
This is the actual statement that prints text to the console.
- System: A built-in class in Java’s standard library.
- out: A static object of the PrintStream class, contained inside System. It represents the standard output stream (usually the console).
- println: A method of PrintStream that prints the message passed to it and then moves the cursor to a new line.
- “Hello, Java!”: The text we want to display. Text enclosed in double quotes is called a string literal in Java.
Note: When you run this program, the output will be:
Hello, Java!
Complete Flow of Execution
- The Java compiler (javac) compiles the HelloWorld.java file into HelloWorld.class (bytecode).
- The JVM executes the bytecode, starting with the main method.
- Inside the main method, it executes the statement System.out.println(“Hello, Java!”);.
- The message Hello, Java! is displayed on the screen.
Key Points to Remember
- Every Java program must have a class, and the file name should match the class name.
- The main method is the starting point of the program.
- System.out.println() is used to print messages to the console.
- Java is case-sensitive: Main, main, and MAIN are all different identifiers.
This diagram shows how the program is divided into:
- Class (HelloWorld)
- Method (main)
- Statement (System.out.println)
With this program, you’ve taken your first step into Java programming. From here, you’ll build on these basics to learn variables, data types, loops, and object-oriented programming.