Your First Java Program
This lesson takes you through writing your first Java program and understanding how Java programs are structured and executed.
Hello World
The first step for every programmer learning a new language is writing Hello World.
Example: Hello World
JAVA
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Output:
TEXT
Hello, World!
Code Breakdown
| Code | Description |
|---|---|
public class HelloWorld |
Defines a class named HelloWorld |
public static void main(String[] args) |
Program entry point (main method) |
System.out.println(...) |
Prints content with a newline |
{ } |
Code block, wrapped in curly braces |
; |
Statement terminator, every statement must end with a semicolon |
⚠️ Note: Java is case-sensitive.
System cannot be written as system, and main cannot be written as Main.
Compile and Run Process
Java programs must be compiled before they can run.
Step 1: Write the Source Code
Create a file named HelloWorld.java (the filename must match the class name).
Step 2: Compile
BASH
javac HelloWorld.java
A successful compile generates a HelloWorld.class bytecode file.
Step 3: Run
BASH
java HelloWorld
⚠️ Note: Don't add the
.class extension when running. Just use the class name.
Complete Process
BASH
# Compile
javac HelloWorld.java
# Run
java HelloWorld
# Output: Hello, World!
Program Structure Explained
Class Definition
JAVA
public class HelloWorld {
// Class body
}
public: Access modifier, means publicly accessibleclass: Keyword, indicates a class definitionHelloWorld: Class name, must match the filename{ }: Class body, contains methods and attributes
Main Method
JAVA
public static void main(String[] args) {
// Method body
}
public: Publicly accessiblestatic: Can be called without creating an objectvoid: No return valuemain: Method name, fixed as the program entry pointString[] args: String array parameter
💡 Memory tip:
public static void main(String[] args) — This is the "front door" of every Java program. Every program must have it.
Output Statements
JAVA
// Print with newline
System.out.println("Hello");
// Print without newline
System.out.print("Hello");
// Formatted output
System.out.printf("Name: %s, Age: %d", "Alice", 25);
JShell Interactive Learning
JShell is Java's interactive tool. It lets you execute code snippets directly without writing a complete class.
JAVA
// Direct calculation
jshell> 5 * 3
$1 ==> 15
// Define a variable
jshell> String name = "Alice"
name ==> "Alice"
// Print output
jshell> System.out.println("Hello, " + name)
Hello, Alice
// Define a method
jshell> int add(int a, int b) {
...> return a + b;
...> }
| created method add(int,int)
// Call the method
jshell> add(3, 4)
$2 ==> 7
💡 Advantage: JShell doesn't require writing
public class or the main method. It's perfect for quickly testing code snippets.
Comments
Comments are for developers to read. The compiler ignores them.
Single-line Comments
JAVA
// This is a single-line comment
int age = 25; // End-of-line comment
Multi-line Comments
JAVA
/*
* This is a multi-line comment
* You can write many lines
*/
Documentation Comments
JAVA
/**
* Calculates the sum of two numbers
* @param a The first number
* @param b The second number
* @return The sum of a and b
*/
public int add(int a, int b) {
return a + b;
}
❓ Frequently Asked Questions
Q Why must the class name match the filename?
A This is a Java specification. A public class must have the same name as its file, otherwise compilation will fail.
Q Can I rename the main method?
A No. The main method is the program entry point recognized by the JVM. Its name and signature are fixed.
Q Can I omit the semicolon?
A No. In Java, every statement must end with a semicolon. This is a syntax rule.
📖 Summary
- Java programs are made up of classes, and the main method is the program entry point
- Use
javacto compile andjavato run - The filename must match the public class name
- JShell lets you execute code snippets interactively
- There are three types of comments: single-line, multi-line, and documentation
📝 Exercises
- Hello World: Write and run your first Java program that outputs "Hello, Java!"
- Personal info: Write a program that prints your name, age, and city (using multiple println statements)
- JShell practice: Use JShell to calculate: If you save 1000 per month, how much can you save in a year?
Next Lesson
In the next lesson, we'll learn about Variables and Data Types — understanding how data is stored in Java.



