Java First Program
In this page:
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
// 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.");
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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.");
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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");
}
}
Login to try C/C++/Java/PHP code in the editor
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
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.");
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
- Naming the file differently from the public class, so
public class HelloinMain.javafails to compile. - Writing the main method signature wrongly, such as
public void main(String[] args), so the JVM cannot find the entry point. - Writing
system.out.printlnwith a lowercases, which is a compile error because the class isSystem.
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: