What Java Code Looks Like
Java programs are written as text files containing code. The code is organized into building blocks with a clear structure: a class that contains methods, including a special method named main that acts as the starting point when you run a program. Inside methods, you write statements (instructions) that the computer executes in order.
At the beginning, you will often write small console programs that print text to the terminal (console). This is a practical way to learn the structure of Java code and how to run it.
A Minimal Java Program
Here is the smallest common Java program you can run. Read it first, then we will break it down line by line.
public class Main { public static void main(String[] args) { System.out.println("Hello, Java!"); }}The Class
public class Main { ... } defines a class named Main. A class is a container for code. For now, think of it as the “file-shaped” unit that holds your program’s methods.
publicmeans the class is accessible.classintroduces a class definition.Mainis the class name.- The curly braces
{and}wrap the contents of the class.
The main Method (Program Entry Point)
public static void main(String[] args) is the method Java looks for to start running your program.
Continue in our app.
You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.
Or continue reading below...Download the app
public: Java must be able to call it from outside the class.static: Java can call it without creating an object first.void: the method returns no value.main: the special name recognized as the entry point.String[] args: an array of command-line arguments (you can ignore it for now, but keep it in the signature).
Statements
Inside the method body, you write statements. A statement is a single instruction that usually ends with a semicolon ;.
This statement prints a line of text to the console:
System.out.println("Hello, Java!");System.outrepresents the standard output stream (the console).printlnprints the text and then moves to a new line.- The text is inside double quotes, which creates a String literal.
- The semicolon ends the statement.
Write and Run Your First Program (Step-by-Step)
1) Create the File
Create a file named Main.java. The file name should match the public class name (Main) exactly, including capitalization.
Put this code into Main.java:
public class Main { public static void main(String[] args) { System.out.println("Hello, Java!"); }}2) Compile the Program
Open a terminal in the folder that contains Main.java and compile:
javac Main.javaIf compilation succeeds, Java creates a file named Main.class (bytecode).
3) Run the Program
Run the compiled class:
java MainYou should see:
Hello, Java!Modify the Program: Change Text and Add Multiple Lines
Change the Printed Text
Edit the string inside println:
public class Main { public static void main(String[] args) { System.out.println("Hello from my first Java program!"); }}Compile and run again:
javac Main.javajava MainAdd Multiple Print Lines
You can add more statements. Java runs them top to bottom:
public class Main { public static void main(String[] args) { System.out.println("Line 1: Starting the program"); System.out.println("Line 2: Printing another message"); System.out.println("Line 3: Done"); }}Compile and run to see three lines of output.
println vs print
println prints a line and then moves to the next line. print does not add a new line. Compare:
public class Main { public static void main(String[] args) { System.out.print("A"); System.out.print("B"); System.out.println("C"); System.out.println("D"); }}This output will be:
ABCDBecause println("C") ends the line after printing C, D appears on the next line.
Common Beginner Errors and How to Read Compiler Messages
When something is wrong, the compiler prints an error message that usually includes a file name, a line number, and a short description. Your job is to use the line number to find the area of code, then look for a small syntax mistake nearby.
Missing Semicolon
If you forget a semicolon at the end of a statement:
public class Main { public static void main(String[] args) { System.out.println("Hello") }}You may see an error like:
Main.java:3: error: ';' expectedMeaning: on or near line 3, the compiler expected a semicolon. Add ; after the statement.
Mismatched or Missing Braces
If you forget a closing brace }:
public class Main { public static void main(String[] args) { System.out.println("Hello, Java!"); }You may see an error like:
Main.java:4: error: reached end of file while parsingMeaning: the compiler got to the end of the file but was still expecting more code (often a missing }). Check that every { has a matching }.
Wrong File Name or Class Name
If the public class name and file name do not match, for example the file is MyProgram.java but the code says public class Main, you may see:
error: class Main is public, should be declared in a file named Main.javaFix by renaming the file to Main.java or changing the class name to match the file name.
Understanding the Error Format
- File and line number: tells you where the compiler noticed the problem (the real cause can be slightly above that line).
- Error description: a hint such as
';'expected,cannot find symbol, orreached end of file while parsing. - Pointer/caret: some outputs show a
^under the approximate location.
A practical approach is: go to the line number, check for missing punctuation (semicolons, parentheses, braces), then recompile. Fix one error at a time, because one small syntax issue can cause multiple follow-up errors.