Java Syntax and Your First Program
Learn to read a Java program as a few meaningful parts, not a wall of punctuation. Start with two messages, trace the statements, make small changes, and use compiler feedback to repair mistakes.
Before you begin: read Java Introduction and JDK and Editor Setup. You can run the examples here in the configured local preview, or use a Java 21 JDK and Main.java on your own computer. No object-oriented design experience is required.
What you will understand
- Identify the class, main method, and statements in a complete program.
- Explain what braces, parentheses, double quotes, and semicolons do.
- Predict the order of simple printed messages.
- Distinguish text from an expression Java calculates.
- Repair a small syntax error using compiler feedback.
- Write a short program from requirements rather than copying an answer.
Syntax is how you make an instruction precise
In everyday language, a classmate can often guess what you mean even if a sentence is incomplete. A compiler cannot safely make that kind of guess. Java syntax is the structure that tells it where a name, value, operation, or body begins and ends. Learning syntax means recognizing a few useful patterns, not memorizing every Java rule at once.
Our goal is modest: write a program that prints a short study message, explain its parts, and make a deliberate change. There is no user input, network call, or hidden database. The source describes the messages, and the program writes them to standard output when main runs.
Start with the meaning of the output. Then look at the structure needed to produce it. This keeps the punctuation connected to a purpose instead of turning the first lesson into a list of symbols.
Read the complete program before editing it
The example below is a complete Main.java file. Before selecting Run code, predict the two lines it will print. The words inside double quotes are the messages. The surrounding Java structure tells the compiler where those messages belong and how to print them.
Run it once without editing. Compare the actual terminal output with the expected output underneath. Then change only the second message and run again. You should see your new text on the second line, while the first line stays the same. A single controlled change makes the cause and effect easier to understand.
Main.java · A welcome message
Predict the result, edit Main.java, then run your program.
Standard input · optional
Select Run code to compile and execute this program.
Welcome to the Java study room
One clear step at a timeRead the structure from the outside inward
First locate the class declaration. Inside the class, locate the main method. Inside main, locate the two printing statements. This gives you three levels to read: the program's named container, its starting method, and the instructions executed by that method.
The braces do not all mean exactly the same thing: one pair surrounds a class body, another a method body. What matters now is matching each opening brace to its closing partner. Use your editor's brace highlighting and keep each closing brace aligned with the construct it closes.
The explanations below give each unfamiliar piece one small job. You do not need to create objects, design an API, or use command-line arguments to complete this lesson. Those ideas become easier when you have a concrete reason to use them.
public class Main- A named container for this program. class introduces a class declaration, and Main is the name we chose. public makes this top-level class accessible. In this example, the public class name and Main.java filename must match exactly. Later you will design classes to model information and behavior; today this one holds our entry point.
public static void main(String[] args)- Where this example starts running. This is the conventional entry-point method we use throughout the beginner examples. Keep the line intact while practising the statements inside it. public makes it accessible, static means it can be called without first constructing a Main object, and void means the method returns no value. Returning a value and printing a message are different operations.
String[] args- A parameter for command-line arguments. The launcher can pass text arguments into this parameter. Our program does not read them, so you do not need to supply any. args is a parameter name, not a magic keyword; the array type and conventional entry-point structure matter. Arrays and parameters get their own lessons later.
{ and }- The beginning and end of a body. The outer pair contains the class body. The inner pair contains main's body. To find a missing closing brace, first identify which opening brace it should match. A method body is a kind of block; adding indentation makes these boundaries easier to see but does not create them.
System.out.println("One clear step at a time");- An instruction to print a line. System.out refers to standard output. println is the operation we call; the parentheses contain its argument. The quoted text is a String value. The final semicolon ends this expression statement. You can use the operation now without knowing how the library implements its output stream.
Four kinds of punctuation, four different jobs
Curly braces group bodies. Parentheses in println(...) hold the argument passed to that call. Double quotes delimit a text value. A semicolon ends the printing statement. These symbols are not interchangeable decorations: replacing one with another changes the program's grammar.
The parentheses after main serve a different role from those after println: main's declaration describes its parameter, while println's call supplies an argument. You can recognize the difference by asking whether the code is defining a method or calling an existing operation.
Do not use the shortcut that every Java line needs a semicolon. In these examples, the printing statements need one, while the class and method declarations use bodies enclosed in braces. A physical line is a formatting choice; a statement is part of Java's structure. Read the construct before adding punctuation.
- Braces: what body contains these instructions?
- Parentheses: what parameter is declared, or what argument is passed?
- Double quotes: what characters are text?
- Semicolon: where does this statement finish?
Trace the statements in order
For this straight-line example, start at the first statement inside main and move downward one statement at a time. Each println writes its argument followed by a line ending. The source file is not printed automatically; only the output operations that run produce these messages.
Predict all three lines before running the example. Next, move the final printing statement above the first one. The printed messages should follow their new order. This is a useful tracing exercise: explain what each operation does before moving to the next.
Later, conditions, loops, method calls, and failures can change the path through a program. We are not using those constructs here. Do not assume every program is simply read top to bottom; practise that model only for the straight-line body you can see in this example.
Main.java · Follow the statements
Predict the result, edit Main.java, then run your program.
Standard input · optional
Select Run code to compile and execute this program.
1. Read the question
2. Predict the result
3. Run and compareCheck the reordered program prediction
If you move the final printing statement above the first, the messages begin with 3. Run and compare, followed by 1. Read the question and 2. Predict the result. Numbers inside messages do not control execution; the position of the statements does.
Decide whether Java should print text or calculate
A quoted message is text, even when its characters look like a calculation. The first statement below prints the characters 6 - 2. The second supplies an arithmetic expression, so subtraction happens before the resulting number is printed.
The third statement combines a label and a calculated value. Parentheses around 6 - 2 make the intended calculation clear before its result is joined to the text. You do not need to store the value in a variable for this small example; variables will get a dedicated lesson.
Change the numbers in the second and third statements to 9 and 4, leaving the first message unchanged. Predict which output lines change. If a line still displays the old characters, check whether you edited a text value or an expression rather than assuming Java ignored your work.
Main.java · Text or calculation?
Predict the result, edit Main.java, then run your program.
Standard input · optional
Select Run code to compile and execute this program.
6 - 2
4
Tasks left: 4Check the changed-number prediction
Leaving the first quoted message unchanged keeps the first line as 6 - 2. Changing the other calculations to 9 - 4 makes the second line 5 and the third line Tasks left: 5. Java does not calculate the characters inside the first message.
Capitalization matters in code, not in the meaning of a message
Java names are case-sensitive. Main and main are different names; println and Println are different names too. In our conventional entry point, main is lowercase. The String type starts with an uppercase S, and the standard System class starts with an uppercase S. These are specific names, not an instruction to capitalize every Java word.
There is another important distinction: changing the text inside quotes changes a message, not an identifier. Printing the word Main does not declare a class. Printing the word main does not call the entry point. Ask whether the characters are inside a String value or being used as Java code.
Try changing println to Println in a runnable example. Compilation should fail because that method name is not found. Restore it before the next experiment. Then change the main method name to Main: the source can compile as an ordinary method, but the launcher will not find the conventional main entry point. A compile failure and a launch failure are different clues.
Make the same behavior easier to read
Both programs below print the same message. The first is compressed onto one line. The second separates the class, method, and statement with line breaks and indentation. Run both to compare behavior, then compare how quickly you can identify the matching braces.
Whitespace between tokens often gives you formatting freedom, but that does not mean spaces are meaningless everywhere. Removing the separation between public and class changes the tokens. Changing spaces inside a quoted message changes the message itself. Reformat deliberately rather than deleting every space.
Use one statement per line and consistent indentation as a beginner habit. It makes errors easier to locate and explanations easier to follow. A formatter can organize already-correct structure, but indentation cannot repair a missing brace, quote, or semicolon.
Main.java · Legal but difficult to scan
Predict the result, edit Main.java, then run your program.
Standard input · optional
Select Run code to compile and execute this program.
Readable code mattersMain.java · The same behavior, easier to read
Predict the result, edit Main.java, then run your program.
Standard input · optional
Select Run code to compile and execute this program.
Readable code mattersConnect the source to the file and launch command
To run these examples yourself, save one complete program as Main.java in the practice folder from the setup lesson. The public class is Main, so the filename must match that name, including capitalization. Run the two terminal commands below from that folder, compiling successfully before launching.
The compiler reads Main.java and produces Main.class for these examples. The launcher command java Main identifies the class to start. Do not append .class to that class name. After editing the source, save and compile again so the launcher does not run an old class file.
If your local result differs from the on-page result, compare the exact saved source, selected JDK, and working folder. The embedded runner uses a separate Java 21 environment; it cannot inspect your own terminal. The setup lesson has the installation and path checks you can use to narrow down a tool problem.
javac Main.java
java MainRepair one mistake using the compiler's clue
This program is intended to print the same welcome messages as before. One statement is missing punctuation. Run it to read the compiler diagnostic, identify the construct that is incomplete, and make the smallest repair.
Main.java · Find the missing punctuation
Predict the result, edit Main.java, then run your program.
Standard input · optional
Select Run code to compile and execute this program.
Reveal the smallest repair and explanation
Add the semicolon after the second println call. The semicolon belongs after the closing parenthesis, not inside the quoted message. The compiler often points near where it discovered the incomplete statement. Always inspect nearby source, not just the highlighted character.
After the repair, the two lines should match the original welcome example. Try one deliberate mistake at a time: remove a quote, restore it, then remove a closing brace. Read each new diagnostic without leaving several unrelated errors in the same experiment.
Independent lab: build a student practice card
Write a small study card for another student. Start from a blank Main.java or replace the source in an editor below. Use only the class, main method, printing calls, text, and arithmetic you have seen in this lesson.
- Use public class Main and the conventional main method.
- Print Java practice card on the first line.
- Print Focus: understand program structure on the second line.
- Print Tasks left: 4 on the third line, calculating 6 - 2 rather than putting the number 4 directly in the message.
- Use one statement per line and consistent indentation.
- Predict the output before running; then change the calculation to 8 - 3 and predict the new final line.
Main.java · Your practice card
Predict the result, edit Main.java, then run your program.
Standard input · optional
Select Run code to compile and execute this program.
Show hints without the solution
- Keep the class and main structure from the welcome example; replace only the statements inside main.
- Use double quotes for the two messages. For the final line, join a text label to the parenthesized subtraction.
- Check matching braces first, then parentheses, quotes, and semicolons.
Reveal the explained lab solution
Main.java · Practice card solution
Predict the result, edit Main.java, then run your program.
Standard input · optional
Select Run code to compile and execute this program.
Java practice card
Focus: understand program structure
Tasks left: 4The first two arguments are String values. The third expression subtracts first and joins the result to a label. With 8 - 3, only the last line changes to Tasks left: 5. The comment in the starter is an instruction for you, not a statement that prints anything. This program uses fixed values; it does not ask the student for input or remember progress.
Review your work
- Can you identify the class body and main body without running the code?
- Does each statement have the intended argument and closing punctuation?
- Can you explain every output line and predict the changed calculation?
- Can you make a small mistake, read the first diagnostic, and repair its cause?
Common errors and how to read them
A diagnostic is evidence about what the tools could not accept or launch. Start with the first useful message and inspect the surrounding source. Later errors can be consequences of the first missing quote or brace, so fix one cause and compile again.
';' expected
Inspect the incomplete statement and the line just before the reported position. A printing statement ends after its closing parenthesis with a semicolon. Do not add semicolons randomly to every line or place one inside the text value.
unclosed string literal
Check the opening and closing double quotes on the message. Use ordinary straight quotes, not typographic smart quotes. Keep this lesson's ordinary String literals on one source line; a visual editor wrap is different from inserting a newline inside the literal.
reached end of file while parsing
A body may be missing a closing brace. Match the opening brace of the class and the opening brace of main with their partners. Work outward from the innermost incomplete body instead of adding a brace without identifying what it closes.
cannot find symbol
Check the exact spelling and case of names such as System, String, and println. Java cannot automatically reinterpret Println as println. Read which name the diagnostic identifies before changing unrelated code.
Main method not found
Compilation can succeed when you accidentally create a method named Main instead of main. Restore the conventional entry-point declaration used in this lesson, then compile again. Changing the printed message cannot fix a missing entry point.
A valid program prints the wrong message
The compiler can accept a statement whose text or calculation does not match your requirements. Compare the output with your prediction line by line. This is a behavior mistake, not necessarily a syntax problem.
Questions students often ask
Must every Java program use a class named Main?
No. Main is the convention used by this lesson's single-file runner. With another public top-level class name, the source filename and launch target must match that name. Keep Main here so your experiments fit the existing runner.
Why is there so much structure around two messages?
The class and entry-point method give these Java 21 examples a conventional starting structure. You can reuse it while learning statements. Other Java versions and tools may support alternative forms; we are deliberately keeping one non-preview form consistent throughout the beginner lessons.
Do blank lines and indentation change the output?
Reformatting between tokens usually does not change these examples. Spaces inside quoted messages are part of the output, and required separation between tokens must remain. Use indentation for clarity without treating it as a replacement for braces.
Do I need to memorize public, static, and String[] today?
Understand their small roles, but do not stop practising until you can recite every detail. First be able to find main, trace its statements, and explain the output. Methods, objects, types, and arrays will return with focused exercises later.
A short checklist to remember
- Find the class, then main, then the statements inside main.
- Match braces, parentheses, and quotes before guessing at a repair.
- Use semicolons where the statement requires them, not after every physical line.
- Respect the spelling and case of identifiers.
- Predict behavior: correct syntax alone does not prove the output is right.
The next lesson focuses on output and comments: making results clear and explaining intent. For now, a small program you can read, change, and repair is better progress than a large one you cannot explain.
Can you read and repair the structure?
Answer all six questions, then review the explanations. Results are temporary on this page; this check does not save course progress.
References and next steps
The study-room examples, exercises, and explanations are original SovranCode material. Official references support the technical rules; the language specification is optional deeper reading, not something to memorize before practising.
- Dev.java — first Java code
- Java SE 21 language specification — blocks and statements
- Java SE 21 language specification — lexical structure
- Java SE 21 launcher reference
Related lessons: Java Introduction · JDK and Editor Setup · Full Java course outline.
Last updated: