Your First Java Program
Hello World and program structure
The "Hello, World!" Architecture
Unlike scripting languages where you can execute a generic print statement on line one, Java operates on an extremely rigid structural design. Every line of code must belong inside a Class. Understanding this initial boilerplate is critical to your entire Java journey.
Important Rule: In Java, the name of your public Class HelloWorld MUST perfectly match the name of the file HelloWorld.java on your file system.
Line-by-Line Breakdown
public class HelloWorld
Class Declaration
Defines an outer structural wrapper. public dictates this class is visible to JVM architectures. class is the core container keyword.
public static void main(String[] args)
The Execution Entry Point
When running Java programs, the JVM actively searches the entire project blindly for this exact signature. If it doesn't find it, execution mathematically cannot begin. static allows JVM to invoke this without instantiating objects.
System.out.println("Hello");
Standard Print Statement
Requests access to the underlying OS System's out (Console/Terminal pipe) variable to print characters ending in a fresh line (ln).
Compilation and Execution Flow
javac HelloWorld.java (Compilation Segment)
Invoke the raw Java Compiler targeting your text file. Assuming absolutely zero syntax errors hit, it will invisibly generate a HelloWorld.class byte file directly in that folder.
java HelloWorld (Execution Segment)
Invoke the embedded JVM engine targeting the specific classname (no extensions). The JVM will boot, load the Bytecode .class into memory arrays, locate the main entry point string, and process machine output.
Java's Foundational Data Building Blocks
Java variables act as strongly-typed memory silos. There are exactly 8 root Primitive Target Types structurally embedded deeply into Java memory blocks:
int
Integer (32-bit)
double
Decimal (64-bit)
boolean
True/False Flag
char
Single Letter
Next Chapter
Jump formally into Control Flow mechanisms (If, Else, Switches, Object Loops) parsing Java logic.
Code Examples
The quintessential Hello World program. Save this as HelloWorld.java, compile with javac, and run with java.
1public class HelloWorld {
2 public static void main(String[] args) {
3 System.out.println("Hello, World!");
4 }
5}Commands to compile and run your first Java program. The output will be: Hello, World!
1# Compile the program
2javac HelloWorld.java
3
4# This creates a HelloWorld.class file
5
6# Run the program
7java HelloWorldA more complex program showing class structure, methods, and reusability. The main method calls the add method which returns the sum of two integers.
1public class Calculator {
2 public static void main(String[] args) {
3 int sum = add(5, 10);
4 System.out.println("The sum is: " + sum);
5 }
6
7 public static int add(int a, int b) {
8 return a + b;
9 }
10}Demonstrates different primitive data types in Java: int, double, char, and boolean.
1public class DataTypesExample {
2 public static void main(String[] args) {
3 int age = 25;
4 double height = 5.9;
5 char initial = 'J';
6 boolean isStudent = false;
7
8 System.out.println("Age: " + age);
9 System.out.println("Height: " + height);
10 System.out.println("Initial: " + initial);
11 System.out.println("Is Student: " + isStudent);
12 }
13}Example of a reference type - String. Strings are objects in Java, not primitive types.
1public class StringExample {
2 public static void main(String[] args) {
3 String greeting = "Hello, Java!";
4 System.out.println(greeting);
5 }
6}This code will NOT compile. Java requires local variables to be initialized before use. This is a common mistake for beginners.
1public class ErrorExample {
2 public static void main(String[] args) {
3 int value; // declared but not initialized
4 System.out.println(value); // error: variable value might not have been initialized
5 }
6}Demonstrates conditional statements (if-else). The program checks the score and prints the appropriate grade.
1public class ConditionalExample {
2 public static void main(String[] args) {
3 int score = 85;
4
5 if (score >= 90) {
6 System.out.println("Grade: A");
7 } else if (score >= 80) {
8 System.out.println("Grade: B");
9 } else {
10 System.out.println("Grade: C");
11 }
12 }
13}For loop example that iterates 5 times, printing the iteration number each time.
1public class LoopExample {
2 public static void main(String[] args) {
3 for (int i = 0; i < 5; i++) {
4 System.out.println("Iteration: " + i);
5 }
6 }
7}While loop example that executes as long as the condition (count < 5) is true.
1public class WhileLoopExample {
2 public static void main(String[] args) {
3 int count = 0;
4 while (count < 5) {
5 System.out.println("Count: " + count);
6 count++;
7 }
8 }
9}Exception handling with try-catch. Instead of crashing, the program catches the ArrayIndexOutOfBoundsException and prints a friendly message.
1public class ExceptionExample {
2 public static void main(String[] args) {
3 try {
4 int[] numbers = {1, 2, 3};
5 System.out.println(numbers[5]); // This will cause an ArrayIndexOutOfBoundsException
6 } catch (ArrayIndexOutOfBoundsException e) {
7 System.out.println("Array index is out of bounds!");
8 }
9 }
10}Use Cases
- Learning basic Java syntax, program structure, and compilation process
- Understanding class-based structure and the main method entry point
- Mastering primitive and reference data types for variable declarations
- Implementing control flow with conditionals and loops
- Practicing proper exception handling for robust applications
- Building foundational skills for creating reusable methods and organized code
Common Mistakes to Avoid
- File name not matching the public class name (Java is case-sensitive)
- Forgetting semicolons at the end of statements
- Missing curly braces or mismatched brackets
- Incorrect main method signature - must be exactly: public static void main(String[] args)
- Not initializing local variables before using them
- Creating infinite loops by forgetting to update the loop condition
- Catching generic Exception instead of specific exception types
- Using = (assignment) instead of == (comparison) in conditionals