HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to JavaVariablesData TypesOperatorsStringsArraysLists (ArrayList)Conditions (if / switch)Loops (for / while)MethodsClasses (OOP)
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array & List
🔁 Condition & Loop
🧩 Method & Class
Java›Introduction to Java

Introduction to Java

First step into enterprise-standard Java: program structure (class/main), System.out.println, and variables.

Simple enough for a 5-year-old to understand

The structure of a Java program

Every Java program is inside a class and runs from the main method. The public class in the file must be named Main.

Your first program
1
public class Main {
Program's class.
DefinitionProgram's class.
2
public static void main(String[] args) {
The program starts here.
FunctionThe program starts here.
3
System.out.println("Hello!");
Prints to the screen.
Output / InputPrints to the screen.
4
}
End of main.
FunctionEnd of main.
5
}
end of class.
Definitionend of class.
DefinitionFunctionOutput / Input
💡 Hint: This skeleton (class + main) is the starting point of every Java program. Every statement ends with ;.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (2 left)

1.From which method does a Java program start executing?
2.Print exactly Hello World to the screen.

🎯 Expected output: Hello World

Loading editor…

About this topic

Even the smallest program in Java contains a class and a main method. At first it might seem like overkill — why so much code just to print a single line? The answer is: Java was designed for large projects, and the same rule prevents structure from falling apart in a system of a hundred thousand lines.

Java is a compiled language, but it compiles not directly to the processor, but to bytecode that the Java Virtual Machine understands. That's why the same compiled file runs on Windows, Linux, and macOS — hence the slogan "write once, run anywhere".

The program starts from the main method. The signature of this method is fixed: public, static, void, and a String array parameter. The virtual machine finds the program by looking precisely for this signature; if you change a single letter, the program won't start.

What you will learn

  • Setting up the class and main method structure
  • Printing output with System.out.println
  • why the semicolon is mandatory
  • Why the file name and class name must be the same
  • Compilation and execution steps

When to use?

println is also the most widely used debugging tool in Java. In large projects, logging libraries are used instead because you can adjust the log level and redirect output to a file; but during learning and quick checks, println is enough.

Common mistakes

Wrong

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

There is no semicolon at the end of the line; in Java, every statement ends with a semicolon. The compiler throws "error: ';' expected" and shows the line number.

Correct

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

Wrong

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

The method name must be main, not Main. Because Java is case-sensitive, the virtual machine cannot find the entry point and throws the "Main method not found in class Main" error.

Correct

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

Wrong

// file name: program.java
public class Main {
    public static void main(String[] args) {}
}

The name of a public class must match the file name exactly. The compiler throws the error "class Main is public, should be declared in a file named Main.java".

Correct

// file name: Main.java
public class Main {
    public static void main(String[] args) {}
}

Frequently asked questions

What is the difference between the JDK and the JRE?

JRE contains only what is necessary to run Java programs. JDK adds the compiler and development tools to this. If you are going to write code, you need to install the JDK; the JDK already includes the JRE within it.

Why is the main method static?

Because no objects have been created yet when the program starts. static tells us that the method belongs to the class itself, not to an object; thus, the virtual machine can call the method without creating an object.

Are Java and JavaScript the same thing?

No, they have no relation. The similarity in names was entirely due to marketing in 1995. Java is a compiled, strongly typed, class-based language; JavaScript is an interpreted, loosely typed language running in the browser. Code written for one will not run in the other.

Summary

In Java, every code lives inside a class, and the program starts from the main method; the signature of this method is fixed. The requirement that the file name and public class name must be the same, and that every statement ends with a semicolon, are two immutable rules of the compiler.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code