Your First Program
Let's take a Java program apart line by line. You'll learn the compact form that Java 25 introduced for small programs, and the traditional form with a class and a public static void main method that you'll see in almost all existing Java code.
The compact form
void main() { System.out.println("Hello, World!"); }
Hello, World!
void main()declares a method calledmain— the place where the program starts.voidmeans it doesn't return a value.- The curly braces
{ }contain the method's body. System.out.println(…)prints a line of text to the console.- The statement ends with a semicolon
;.
A file like this is a compact source file (Java 25+). Behind the scenes Java wraps it in a class for you.
The traditional form
public class Main { public static void main(String[] args) { System.out.println("Hello, World!"); } }
Hello, World!
This version works in every Java version, and it's what most tutorials, books and real projects use:
public class Main— all Java code lives inside classes. A public class must be in a file with the same name:Main.java.public static void main(String[] args)— the classic entry point.publiclets the JVM call it,staticmeans it belongs to the class rather than an object, andString[] argsreceives command-line arguments.
You'll learn what each keyword means in the object-oriented lessons. Until then, this course mostly uses the compact form so examples stay short — but both are real, complete Java programs.
print vs println
void main() { System.out.print("Hello, "); System.out.print("World"); System.out.println("!"); System.out.println("Next line"); }
Hello, World! Next line
println moves to a new line after printing; print doesn't. Java 25 also adds a shorter IO.println(…) and IO.print(…) — handy for small programs. This course sticks to System.out, which works everywhere.
Printing numbers and expressions
void main() { System.out.println(42); System.out.println(3 + 4); System.out.println("3 + 4 = " + (3 + 4)); System.out.println("3 + 4 = " + 3 + 4); }
42 7 3 + 4 = 7 3 + 4 = 34
Look at the last line: once text is involved, + joins things together, so "3 + 4 = " + 3 becomes text and then 4 is appended. Parentheses force the addition first.
Escape sequences
void main() { System.out.println("Line one\nLine two"); System.out.println("Name:\tAna"); System.out.println("She said \"hi\""); System.out.println("C:\\Users\\ana"); }
Line one Line two Name: Ana She said "hi" C:\Users\ana
| Escape | Meaning |
|---|---|
\n | New line |
\t | Tab |
\" | Double quote |
\\ | Backslash |
Compiler errors
void main() { System.out.println("Missing semicolon") }
Main.java:2: error: ';' expected
The compiler reports the file, the line number and what it expected. Reading error messages carefully — starting with the first one — is a core programming skill. Java is also case-sensitive: system.out.println or Main vs main are different names.
Try it yourself
Print a small receipt using print, println and tabs.
Item Price Coffee 2.50 Cake 3.20 Total 5.7