Hello World
In this tutorial, you will learn to write "Hello World" program in Java. Understand the class structure and main method.
A "Hello, World!" is a simple program that outputs Hello, World! on the screen. Since it's a very simple program, it's often used to introduce a new programming language to a newbie.
Let's explore how Java "Hello, World!" program works.
Java "Hello, World!" Program
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}Output:
Hello, World!graph LR
A[HelloWorld.java] -->|javac| B[HelloWorld.class]
B -->|java| C[JVM]
C -->|Output| D[Hello, World!]
style A fill:#e1f5fe,stroke:#01579b
style B fill:#fff9c4,stroke:#fbc02d
style C fill:#fce4ec,stroke:#c2185b
style D fill:#e0f2f1,stroke:#00695cFrom Code to Output
How Java "Hello, World!" Program Works?
1. Your First Program
In Java, any line starting with // is a comment. Comments are intended for users reading the code to understand the intent and functionality of the program. It is completely ignored by the Java compiler.
2. class HelloWorld
In Java, every application begins with a class definition. In the program, HelloWorld is the name of the class, and the class definition is:
class HelloWorld {
... .. ...
}Rule: The name of the public class must match the filename. If the class is public class HelloWorld, the file must be named HelloWorld.java.
3. public static void main(String[] args)
This is the main method. Every application in Java must contain the main method. The Java compiler starts executing the code from the main method.
- public: Access modifier. It means the method is visible to everyone.
- static: It means the method belongs to the class, not an instance of the class.
- void: It means the method does not return any value.
- main: The name of the method.
- String[] args: An array of Strings. It stores command-line arguments passed to the program.
4. System.out.println("Hello, World!");
The code above is a print statement. It prints the text Hello, World! to standard output (your screen).
- System: A built-in class that contains useful members.
- out: A static member of the System class (Standard Output Stream).
- println: Method to print a line.
What is the entry point of a Java application?
Tip 💡: public static void main is a bit of a mouthful! Just memorize it for now. It's the magic spell that starts every Java program.
Command Line Arguments
The String[] args parameter in the main method allows you to pass arguments from the command line.
class Main {
public static void main(String[] args) {
System.out.println("Argument 1: " + args[0]);
}
}If you run this with java Main Hello, it will print Argument 1: Hello.
Challenge
Challenge
Task:
Modify the program to print 'Hello Javapedia'.Key Takeaways
- Class Definition: Every Java program must have at least one class.
- Main Method:
public static void main(String[] args)is the entry point. - Printing:
System.out.println()prints to the console. - Case Sensitive: Java is case-sensitive.
Common Pitfalls
[!WARNING] Case Sensitivity: Java is case-sensitive.
Mainis different frommain.
[!WARNING] Missing Semicolons: Every statement in Java must end with a semicolon
;.
What's Next?
You've written your first program! Now, let's learn how to store and manipulate data using Variables. Learn about Variables & Literals →
Java JDK, JRE and JVM
Complete guide to JDK, JRE and JVM in Java. Learn the differences, relationships, and how they work together in Java development.
Java Variables and Literals
Learn Java variables and literals with examples. Understand variable declaration, initialization, naming rules, and all types of literals in Java programming.
