Java Installation, IDEs & First Hello World Program
JDK 21 LTS Installation ยท Setting JAVA_HOME & PATH ยท IDEs vs Online Compiler ยท First Program Breakdown ยท main() Signature Anatomy ยท System.out.println
Step-by-step walkthrough for configuring modern Java 21 LTS on Windows, macOS, and Linux, setting up professional IDEs like IntelliJ IDEA and VS Code, writing your first Hello World program, and demystifying every single keyword in public static void main(String[] args).
1. Installing Java 21 LTS & Configuring Environment Variables
To build modern Java applications, always download a Long-Term Support (LTS) release like Java 21 LTS or Java 17 LTS from trusted vendors:
- Oracle OpenJDK / Oracle JDK: [oracle.com/java](https://www.oracle.com/java/)
- Eclipse Temurin (Adoptium): [adoptium.net](https://adoptium.net/) (Recommended open-source production distribution)
- Amazon Corretto: [aws.amazon.com/corretto](https://aws.amazon.com/corretto/)
Configuring Environment Variables on Windows:
1. JAVA_HOME: Point this system variable to your JDK root installation folder (e.g.C:\Program Files\Eclipse Adoptium\jdk-21.0.2).
2. PATH: Add %JAVA_HOME%\bin to your existing system Path variable. This allows running javac and java commands from any terminal directory.
Verification in Terminal:
bash
javac -version
# Expected Output: javac 21.0.2
java -version
# Expected Output: openjdk version "21.0.2" ...
2. Choosing Your Development Environment
Modern Java developers use state-of-the-art IDEs equipped with intelligent code completion, automated refactoring, and step-through debuggers:
- IntelliJ IDEA (JetBrains): The gold standard in industry enterprise development. Offers supreme refactoring, Spring Boot integration, and static analysis.
- Visual Studio Code (VS Code): Lightweight, ultra-fast editor powered by Microsoft's "Extension Pack for Java".
- Eclipse IDE: Classic open-source enterprise IDE widely used in corporate and legacy projects.
- Our Compiler Online Java IDE: Instant, zero-install, in-browser compiler for rapid prototyping, algorithm practice, and learning.
3. Anatomy of the First Java Program: Demystifying Every Keyword
Here is the canonical Java Hello World program:
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}Let us dissect every single token and keyword so you understand exactly why Java requires this exact structure:
1. public class Main
- public (Access Modifier): Declares that this class is accessible from any other class and package.
- class (Keyword): Java's fundamental building block used to define a blueprint for objects.
- Main (Identifier): The identifier/name given to this class. Crucial Rule: In Java, if a class is declared public, its file name MUST match the class name exactly with a .java extension (Main.java). Java is strictly case-sensitive!
2. public static void main(String[] args)
This is the mandatory entry point of every Java application. When the JVM starts, it searches specifically for this exact method signature:
- public: The JVM runs outside your program's package; therefore, the entry point method must be publicly accessible so the JVM can call it.
- static: Allows the JVM to invoke this method without needing to create an instance/object of the Main class first (Main.main()). Without static, the JVM would not know how to instantiate your class.
- void: Specifies the method's return type. Since the program terminates when main() finishes, it returns nothing (void) to the operating system.
- main: The reserved method identifier recognized universally by JVM classloaders as the initial entry point.
- String[] args (Parameters): An array of String objects that receives optional command-line arguments passed to the application when executed from the terminal (e.g. java Main server 8080).
3. System.out.println("Hello, World!");
- System: A built-in standard class in the java.lang package containing useful system-level facilities.
- out: A public static final instance of PrintStream inside the System class representing the standard output stream (console).
- println(): A method of PrintStream that prints the passed string argument to the console followed by a newline character (\n).
- ; (Semicolon): Every statement in Java MUST terminate with a semicolon. It tells the compiler where a complete instruction ends.
4. `System.out.println()` vs `System.out.print()` vs `System.out.printf()`
Java provides three standard methods to output data to the console:
| Method | Behavior | Example |
|---|---|---|
| **`print()`** |
System.out.print("Hello "); System.out.print("World"); -> Hello World |
| println() | Outputs text and immediately advances the cursor to the beginning of the next line. | System.out.println("Line 1"); System.out.println("Line 2"); |
| printf() | Formats text using C-style format specifiers (%s, %d, %.2f). | System.out.printf("Name: %s, Score: %d", "Ravi", 95); |
Beginner Example & Code Anatomy
public class Main {
public static void main(String[] args) {
// 1. Using println() for discrete output lines
System.out.println("1. Java 21 LTS initialized successfully.");
System.out.println("2. Outputting data across multiple lines:");
// 2. Using print() for continuous output on the same line
System.out.print(" [Progress: ");
System.out.print("25% -> ");
System.out.print("50% -> ");
System.out.print("100% Complete");
System.out.println("]"); // Closes line with a newline
// 3. Printing numbers and simple expressions
System.out.println("3. Direct Arithmetic Calculation: 10 + 20 = " + (10 + 20));
System.out.println("4. Welcome to Professional Java Engineering!");
}
}
๐ Line-by-Line Code Explanation
public class Main
Class declaration with public accessibility matching Main.java filename.
public static void main(String[] args)
The universal JVM entry point method receiving optional command-line string arguments.
System.out.print(...)
Streams characters to the console without moving the cursor to the next line.
"..." + (10 + 20)
String concatenation combining text with parenthesized arithmetic evaluation result (30).
Practical Real-World Example
public class CommandLineGreeting {
public static void main(String[] args) {
// Checking if command line arguments were passed
if (args.length > 0) {
System.out.println("Received Command-Line Arguments:");
for (int i = 0; i < args.length; i++) {
System.out.println(" Argument [" + i + "]: " + args[i]);
}
} else {
System.out.println("No command-line arguments provided. Defaulting to standard mode.");
System.out.println("Tip: In terminal, run: java CommandLineGreeting DevServer 8080");
}
}
}
- Filename mismatch: Naming the file "test.java" while the code contains "public class Main" causes compile-time error: "class Main is public, should be declared in a file named Main.java".
- Case sensitivity errors: Typing "system.out.println" or "String[] Args" or "main(string[] args)" will fail compilation because Java is strictly case-sensitive.
- Missing semicolon (;): Every statement must end with a semicolon.
- Modifying main method signature: Changing "public static void main(String[] args)" to "private void main()" will compile fine, but running it produces: "Main method not found in class".
Test your understanding by writing the code directly in your editor or running in our online Java compiler:
// Coding Challenge:
// Write a Java program named UserProfileCard that outputs:
// ----------------------------------------
// DEVELOPER PROFILE: [Your Name]
// ROLE: Backend Java Engineer
// FAVORITE TOOL: IntelliJ IDEA & Docker
// ----------------------------------------
// Use formatted System.out.println statements.
public class UserProfileCard {
public static void main(String[] args) {
// TODO: Write your formatted profile card output here
}
}
๐ก Frequently Asked Questions & Interview Insights
โ Can we change "String[] args" to "String args[]" or "String... args"?
Yes! Both "String args[]" (C-style syntax) and "String... args" (varargs syntax) are 100% valid main method signatures accepted by the JVM.
โ Can a Java file contain multiple classes?
Yes, a single .java file can contain multiple classes, but only ONE class can be declared "public", and the file name must match that public class name.
โ What happens if we remove the "static" keyword from main()?
The code will compile without errors, but when you attempt to run it with "java Main", the JVM will throw a NoSuchMethodError because it cannot find a static entry point to call without instantiation.
๐ Quick Chapter Recap
- Java 21 LTS is the recommended modern Long-Term Support release for production.
- Every standalone Java program starts execution at: public static void main(String[] args).
- public class name MUST match the filename (Main.java).
- System.out.println() prints with a newline; System.out.print() prints continuously on the same line.