π Hello World in Java
Letβs write your first Java program β the famous "Hello, World!"
π‘ What Is It?β
βHello Worldβ is the traditional first program in any language. It teaches:
- How to write code
- How to compile Java
- How to run Java
β Fun Factβ
The first βHello, Worldβ in programming was written by Brian Kernighan in 1972 β even before Java was born!
π§ Explain Like I'm Fiveβ
Think of Java like a letter you write, but your computer canβt read it directly. You need to translate (compile) it and then deliver (run) it. This Hello program is like your first βHi!β to the computer. π
ποΈ Code Exampleβ
1. Create a file named HelloWorld.javaβ
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
2. Compile the codeβ
javac HelloWorld.java
If successful, a HelloWorld.class file will be created.
3. Run the compiled fileβ
java HelloWorld
β You should see:
Hello, World!
π Breakdown of the Codeβ
| Code Part | What it Means |
|---|---|
public class HelloWorld | Declares a class (every Java program is inside a class) |
public static void main(String[] args) | Entry point β the first thing Java runs |
System.out.println(...) | Prints to console |
π Real-World Analogyβ
Writing a Java program is like making a cake:
- You write a recipe (Java code)
- You compile it with a chef (compiler)
- You serve the cake (run the output)
π Try It Yourselfβ
π§ͺ Exerciseβ
- Create a new file:
HelloWorld.java - Paste this code:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java Learner!");
}
}
- Compile and run:
javac HelloWorld.java
java HelloWorld
β Output should be:
Hello, Java Learner!
Write your output here: ____________
π What's Next?β
Now that you've made Java speak, it's time to learn about Java Syntax and how Java programs are structured.