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!");
    }
}
▶ Try it Yourself

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
}

Main Method

JAVA
public static void main(String[] args) {
    // Method body
}
💡 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

📝 Exercises

  1. Hello World: Write and run your first Java program that outputs "Hello, Java!"
  2. Personal info: Write a program that prints your name, age, and city (using multiple println statements)
  3. 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.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏