Java Tutorial
🔍
Java ProgramsBasics & I/OHello, World

Hello, World in Java

beginner·  Basics & I/O  ·  Fundamentals

Problem

Hello World is the traditional first program written in any programming language, used to confirm the basic setup works.

Print "Hello, World!" to the console.

Java Program

Java
public class HelloWorld { public static void main(String[] args) { // main() is the entry point the JVM looks for and calls first System.out.println("Hello, World!"); } }

Output

Hello, World!

Core Logic

It's the classic first program in any language — define the method the JVM looks for on startup, then print a line of text.

How It Works
  1. 1public class HelloWorld declares a class matching the file name, which Java requires for the public class in a file.
  2. 2public static void main(String[] args) is the exact method signature the JVM searches for and invokes first.
  3. 3Inside main, System.out.println(...) writes the given string to standard output.
  4. 4println (as opposed to print) appends a newline after the text.
Running the program prints exactly one line: Hello, World!.
💡

Key Point: Every Java program needs this exact main signature — the JVM won't find an entry point without it.

Key Concepts

main methodSystem.out.println()

Approach 2: Using printf()

Java
public class HelloWorldPrintf { public static void main(String[] args) { System.out.printf("Hello, World!%n"); // %n adds the newline printf doesn't add automatically } }

Output

Hello, World!

Core Logic

printf() writes formatted output using a template string, which becomes useful the moment a program needs to mix in variable values.

How It Works
  1. 1System.out.printf("Hello, World!%n") takes a format string as its argument, the same way println takes plain text.
  2. 2%n is printf's placeholder for a platform-correct newline — printf doesn't add one automatically the way println does.
  3. 3With no other % placeholders in the string, this call behaves just like println, just written a different way.
System.out.printf("Hello, World!%n") prints the same single line as println("Hello, World!").
💡

Key Point: printf only pays off once a format string has real placeholders — System.out.printf("Hello, %s!%n", name) substitutes name into %s, something plain println can't do without manual string concatenation.

Key Concepts

System.out.printf()format string%n

Related Programs