← Back to Core Java Course | Chapter 1: Introduction & Basics | Lesson 4 of 12

Java First Program

Your first Java program is like saying hello for the first time. You build a small class with a starting spot called main and tell it to print a message on the screen.
Syntax
java
public class ClassName {
    public static void main(String[] args) {
        // statements
    }
}

The Main Class

Java has no free-floating functions -- every method, including main, has to live inside a class, and by convention the file's public class should share its exact name so the compiler can find your entry point.

Example: The Main Class

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println("main() lives inside a class - Java has no free-floating functions.");
	}
}

The main Method

The JVM specifically looks for public static void main(String[] args) -- get that signature wrong (wrong capitalization, missing static, different parameter type) and Java won't recognize it as the program's starting point.

Example: The main Method

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println("JVM looks for exactly this signature to start the program.");
	}
}

Printing Output

System.out.println() converts its argument to text and writes it to standard output, then appends a newline character so the next println() call starts on a fresh line automatically.

Example: Printing Output

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println("First line");
		// Print a line to the console
		System.out.println("Second line automatically starts fresh");
	}
}

System and Out Classes

System is a built-in class in java.lang, and out is one of its static fields -- a PrintStream object wired to your terminal -- which is why you always write System.out rather than creating your own instance.

Example: System and Out Classes

java
public class Main {
	public static void main(String[] args) {
		java.io.PrintStream out = System.out; // out is a static PrintStream field of the System class
		out.println("Written through System's static 'out' field.");
	}
}

Statement Terminators

Unlike Python, Java requires a semicolon at the end of every statement to mark where it ends; the compiler uses this to parse your code correctly, and omitting one is one of the most common first syntax errors beginners hit.

Example: Statement Terminators

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare a and set it to 5
		int a = 5;
		// Declare b and set it to 10
		int b = 10;
		// Print a line to the console
		System.out.println(a + b);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Naming the file differently from the public class, so public class Hello in Main.java fails to compile.
  2. Writing the main method signature wrongly, such as public void main(String[] args), so the JVM cannot find the entry point.
  3. Writing system.out.println with a lowercase s, which is a compile error because the class is System.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.