For working professionals
For fresh graduates
More
6. JDK in Java
7. C++ Vs Java
16. Java If-else
18. Loops in Java
20. For Loop in Java
46. Packages in Java
53. Java Collection
56. Generics In Java
57. Java Interfaces
60. Streams in Java
63. Thread in Java
67. Deadlock in Java
74. Applet in Java
75. Java Swing
76. Java Frameworks
78. JUnit Testing
81. Jar file in Java
82. Java Clean Code
86. Java 8 features
87. String in Java
93. HashMap in Java
98. Enum in Java
101. Hashcode in Java
105. Linked List in Java
109. Array Length in Java
111. Split in java
112. Map In Java
115. HashSet in Java
118. DateFormat in Java
121. Java List Size
122. Java APIs
128. Identifiers in Java
130. Set in Java
132. Try Catch in Java
133. Bubble Sort in Java
135. Queue in Java
142. Jagged Array in Java
144. Java String Format
145. Replace in Java
146. charAt() in Java
147. CompareTo in Java
151. parseInt in Java
153. Abstraction in Java
154. String Input in Java
156. instanceof in Java
157. Math Floor in Java
158. Selection Sort Java
159. int to char in Java
164. Deque in Java
172. Trim in Java
173. RxJava
174. Recursion in Java
175. HashSet Java
177. Square Root in Java
190. Javafx
If you're learning Java, your journey often starts with a simple "Hello World" program. This basic example introduces you to the structure and syntax of the Java programming language. It helps you understand how a Java class works, how methods are written, and how the program runs.
The "Hello World" program in Java programming is used to display a simple output on the console. Though it seems basic, it builds a foundation for writing complex programs later. In this blog, we'll explore every part of this program in detail and help you understand how it works.
Software engineering courses can help you grasp these concepts more effectively.
In Java, the "Hello World" program is the simplest code you can write. It is mainly used to test whether the development environment is set up correctly. It also helps you understand basic Java syntax like class definition, the main method, and output printing.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
This program prints "Hello, World!" on the screen. It's your first step in learning Java programming.
Unlock a high-paying career with the following full-stack development courses:
When you write and execute a Java Hello World program, several processes take place behind the scenes. Let’s understand each step involved in converting your code into output:
You start by writing a simple Java program using a text editor or IDE. The code typically looks like this:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
This code is saved with the filename HelloWorld.java. The file name must match the public class name (HelloWorld).
You compile the Java source code using the javac compiler:
javac HelloWorld.java
This command converts the human-readable .java file into a .class file containing bytecode. The output file will be:
HelloWorld.class
Bytecode is a low-level, platform-independent set of instructions that the Java Virtual Machine (JVM) understands.
Unlike other languages that compile into machine-specific code, Java compiles into bytecode. This bytecode is not directly executed by your computer’s processor. Instead, it’s run by the JVM, which acts as an interpreter.
This is what makes Java platform-independent — the same .class file can run on any OS (Windows, macOS, Linux) as long as it has a compatible JVM.
Also check: Exploring Java Architecture: A Guide to Java's Core, JVM and JDK Architecture
To run the compiled Java program, use the following command:
java HelloWorld
Here, you don’t include the .class extension. The JVM now loads the HelloWorld.class file and looks for the main() method with this exact signature:
public static void main(String[] args)
This is the entry point of the program.
The JVM performs the following tasks:
In our case, it prints:
Hello, World!
You see the message "Hello, World!" printed on the console, indicating that your Java environment is correctly set up and the code is working as expected.
Also read: Object Class in Java: Methods, Usage, and Examples Explained
Here is the complete code for the Java Hello World program. This example shows the most basic use of Java's class structure and method declaration.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Output:
Hello, World!
Explanation:
This code defines a class named HelloWorld, with a main method that prints the message to the screen.
Also read: Types of Variables in Java: Java Variables Explained
This section breaks down the "Hello World" program line by line. It explains what each part does and why it is important.
public class HelloWorld {
This line defines a public class named HelloWorld. The file name should match the class name.
public static void main(String[] args) {
This is the entry point of every Java program. It tells the JVM where to start execution.
System.out.println("Hello, World!");
This prints the message to the console. System.out is Java's built-in output stream.
Must check: Explore Abstract Class and Method in Java
The main method has a specific structure in Java. Let’s break it down:
public static void main(String[] args)
Changing this signature may result in a runtime error or prevent program execution.
Also explore: Looping Statements in Java: Types, Syntax, Examples & Best Practices
Java offers two methods for output: print() and println(). The println() adds a new line after the message, while print() does not.
System.out.print("Hello, ");
System.out.print("World!");
Output:
Hello, World!
Use println() when you want the cursor to move to the next line after printing.
Explore more: Array in Java: Types, Operations, Pros & Cons
The Java Hello World program is not just for beginners. It's also used to:
The Java Hello World program may be simple, but it plays a vital role in your programming journey. It introduces you to Java’s syntax, structure, and execution process. Understanding each part prepares you for writing more complex applications.
Start with this basic program, and soon you'll be writing full Java applications with confidence.
The main() method is declared static so that the Java Virtual Machine (JVM) can invoke it without creating an instance of the class. This is important because the program starts before any object is created. Making it static ensures that the method can be accessed directly by the JVM at runtime.
Yes, you can rename the HelloWorld class to any valid identifier. However, the filename must exactly match the public class name. For instance, if your class is MyFirstProgram, then the file must be saved as MyFirstProgram.java to compile without errors.
If the main() method is not declared as public, the JVM will not be able to access it from outside the class during program execution. This will result in a runtime error, typically main method not found, as the JVM requires public access to invoke the method.
In Java, when you define a public class, the source file name must match that class name. This rule helps the compiler associate the file with its main class. If the class name and file name differ, the code won't compile and will show a naming mismatch error.
System.out.println() is used to print messages to the console. System is a built-in Java class, out is its output stream, and println() prints the text followed by a newline. It's essential for displaying output or debugging during development or when running a Java application.
Yes, you can have multiple classes in a single Java file, but only one of them can be declared public. That public class must match the filename. Other classes in the same file can be package-private and are typically used for supporting purposes within the same file.
Yes, Java requires a semicolon at the end of each executable statement. It marks the end of the instruction for the compiler. If you forget to add a semicolon, the compiler will throw a syntax error. Only class and method declarations don’t need semicolons directly after them.
Bytecode is the intermediate, platform-independent code generated after compiling a Java program using javac. When you compile the Hello World program, it creates a .class file that contains bytecode. The JVM then executes this bytecode on any platform.
Yes, Java allows method overloading, so you can define multiple main() methods with different parameter types or counts. However, only the method with the exact signature public static void main(String[] args) will be recognized as the entry point when the program starts.
Java’s Hello World program is compiled into bytecode, which can run on any system with a Java Virtual Machine (JVM). This bytecode is the same regardless of the OS, making Java programs like Hello World platform-independent and a key reason for Java’s popularity.
If you don't specify any access modifier for a class in Java, it gets package-private access by default. This means the class is only accessible within the same package. This level of access control helps restrict the visibility of classes and organize large applications into logical units.
Take the Free Quiz on Java
Answer quick questions and assess your Java knowledge
Author|900 articles published
Previous
Next
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.