Lesson 7 of 50 – First Java Program
14%

First Java Program

In this lesson, we will write our first Java program, understand its structure, compile the program, and execute it to display output on the screen.

Learning Objective: Learn how to create, save, compile, and run a simple Java program.
1. Your First Java Program

Write the following code in your editor:

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

Output:

Hello World!

This program displays the message Hello World! on the screen.

2. Understanding the class Keyword

In Java, the class keyword is used to declare a class.

public class Hello {
}

Here:

  • class declares a class.
  • Hello is the class name.
  • public makes the class accessible from other code.
  • Curly braces { } define the class body.

A Java application can contain one or more classes. The class containing the main method can serve as the program's starting point.

3. Understanding the main() Method

The main() method is the standard entry point used to launch a simple Java application.

public static void main(String[] args) {
    // Program statements
}

Meaning of each part:

  • public — allows the launcher to access the method.
  • static — lets it be called without creating an object of the class.
  • void — means the method does not return a value.
  • main — the method name recognized as the entry point.
  • String[] args — receives command-line arguments.
4. Understanding System.out.println()

The System.out.println() statement displays information and then moves to a new line.

System.out.println("Welcome to Java");
System.out.println("Welcome to Soopro Pathshala");

Output:

Welcome to Java
Welcome to Soopro Pathshala

Text displayed using quotation marks is called a string literal.

5. Understanding Semicolons

Most Java statements end with a semicolon (;).

System.out.println("Hello");
System.out.println("Java Programming");

If you forget a required semicolon, the compiler may report a syntax error.

Remember: A class declaration or method declaration does not end with a semicolon after its closing brace.
6. Understanding Curly Braces

Curly braces { } group statements inside classes and methods.

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello Java");
    }
}
  • The first opening brace begins the class body.
  • The second opening brace begins the method body.
  • The first closing brace ends the method body.
  • The last closing brace ends the class body.

Every opening brace must have a matching closing brace.

7. Save Your Java Program

Follow these steps:

  1. Open a code editor.
  2. Write the first Java program.
  3. Click Save As.
  4. Enter the filename Hello.java.
  5. Save the file in a folder you can access from the terminal.
If the file contains public class Hello, save it as Hello.java. The public class name and filename must match, including capitalization.
8. Compile the Program

Open a terminal or command prompt in the folder containing Hello.java.

Run this command:

javac Hello.java

The javac compiler checks the source code and, if compilation succeeds, generates Hello.class.

If the terminal reports that javac is not recognized, check your JDK installation and PATH configuration.

9. Run the Program

After successful compilation, enter:

java Hello

Output:

Hello World!

The java command launches the compiled class. Do not add .class to the class name in this command.

10. Using print() and println()

Java provides both print() and println() for displaying output.

Example using print():

System.out.print("Hello ");
System.out.print("Java");

Output:

Hello Java

Example using println():

System.out.println("Hello");
System.out.println("Java");

Output:

Hello
Java

print() does not automatically move to a new line. println() does.

11. Display Numbers and Calculations

Java can also display numbers and calculation results.

public class Addition {
    public static void main(String[] args) {
        System.out.println(10);
        System.out.println(10 + 20);
    }
}

Output:

10
30

Numbers do not need quotation marks when you want Java to treat them as numeric values.

If you write "10" + "20", Java joins the two strings and produces 1020.

12. Common Errors in the First Program
Error Correction
Missing semicolon Add ; at the end of the statement.
Wrong filename Match the filename with the public class name.
Incorrect capitalization Write Java keywords and identifiers with correct capitalization.
Missing curly brace Check that every opening brace has a closing brace.
Wrong command Use javac Hello.java to compile and java Hello to run.
13. Practice Program and Summary

Practice Task: Write a Java program that displays your name, your institute's name, and your favorite programming language.

public class MyDetails {
    public static void main(String[] args) {
        System.out.println("My Name: Rahul");
        System.out.println("Institute: Soopro Pathshala");
        System.out.println("Favorite Language: Java");
    }
}

Possible output:

My Name: Rahul
Institute: Soopro Pathshala
Favorite Language: Java

Summary:

  • A Java program can contain classes and methods.
  • The main method is the standard application entry point.
  • System.out.println() displays output on a new line.
  • Save a public class in a file with the matching name.
  • Compile with javac and run with java.
Congratulations! You have learned how to write, compile, and run your first Java program.

🧠 Quick Quiz

1. Which method is commonly used as the starting point of a Java application?




2. Which command compiles Hello.java?




3. What is the output of System.out.println(10 + 20);?