Java syntax.
Make the code make sense.
A Java program can look like a wall of unfamiliar words. Let’s break it into small, understandable parts—then change the code and see what happens.
By the end of this lesson, you can…
- Recognize the class, main method, and statements in a conventional Java program.
- Use braces, semicolons, and quoted text correctly.
- Read a short program, predict its output, and fix common syntax mistakes.
Your first complete Java program
Think of this example as a small set of instructions: create a class, find the starting point, and print a message. Save it as Main.java.
public class Main {
public static void main(String[] args) {
System.out.println("Vanakkam, Java!");
}
}Vanakkam, Java!
Main groups the program’s code.You do not need to memorize every keyword today. First understand the shape: a class contains a method, and the method contains statements.
What does each part do?
| Code | Meaning | Remember |
|---|---|---|
public class Main | Declares a public class named Main. | For this example, use the filename Main.java. |
{ } | Encloses a block of code. | One pair surrounds the class; another surrounds the method. |
public static void main(String[] args) | The conventional entry-point method used throughout this beginner course. | The method name is lowercase main. |
System.out.println(...) | Writes a value to standard output, followed by a line break. | System begins with uppercase S; println is lowercase. |
"Vanakkam, Java!" | A string literal: the text to display. | Use ordinary double quotes for strings. |
; | Ends this method-invocation statement. | Place it after println(…), not after the opening class brace. |
Go a little deeper: public, static, void, and args
public makes this method accessible. static means it can be called without creating a Main object. void means it returns no value. String[] args receives command-line arguments as an array of strings. You will explore these ideas in the methods and OOP sections.
A useful distinction: indentation helps people read your code; braces determine the blocks. Modern Java also supports simpler entry-point forms, but this course begins with the conventional class-based form above.
Four syntax habits worth building
1. Match the public class name and filename
With the usual javac workflow, a public top-level class named Main belongs in Main.java. Changing one without changing the other causes a compilation error.
2. Respect uppercase and lowercase letters
name and Name are different identifiers. Likewise, System works here; system does not refer to the same class.
3. Close what you open
Pair every opening brace { with a closing brace }. Keep the parentheses around method arguments, and keep both quotes around a string.
4. End statements, not every line
The call System.out.println("Hello"); ends with a semicolon. A class declaration or method header followed by an opening brace does not need one there.
What will this program print?
Variables give names to values. The plus sign below joins text with another value. Read the statements from top to bottom and predict the two output lines.
public class Main {
public static void main(String[] args) {
String name = "Kavi";
int lessonsCompleted = 3;
System.out.println("Hello, " + name + "!");
System.out.println("Lessons completed: " + lessonsCompleted);
}
}Reveal the output and explanation
Hello, Kavi! Lessons completed: 3
The first statement joins "Hello, ", the value of name, and "!". The second joins a label with the number 3. Each println starts a new output line after printing its value.
Change it. Run it. Understand it.
Load an example below, edit it in the Java editor, and press RUN inside the editor. Try changing the message, the name, or the lesson count. The output comes from executing Java.
Powered by OneCompiler. Code you run is sent to OneCompiler for execution. Use the examples here or your own practice code. If the embed is unavailable, use the new-tab link above.
Three small challenges
Challenge 1 · Make it personal
Load Hello World. Change the message to I am learning Java!. Add a second println that displays your favorite programming topic.
Show one possible solution
public class Main {
public static void main(String[] args) {
System.out.println("I am learning Java!");
System.out.println("My favorite topic is loops.");
}
}Challenge 2 · Spot the mistake
This line has two problems. Try to identify both before opening the answer.
system.out.println("Hello")Reveal the fix
Change lowercase system to uppercase System and add the missing semicolon.
System.out.println("Hello");Challenge 3 · Predict the difference
Replace two println calls with two print calls. What changes?
Reveal the explanation
print does not automatically add a line break. The two messages appear next to each other unless you include spacing or a newline yourself.
When the compiler complains
| What you notice | Check this first |
|---|---|
A message such as ';' expected | Look for a missing semicolon after a statement. |
Cannot find symbol for system | Use System with uppercase S. |
| Public class and filename do not match | Check Main versus Main.java, including letter case. |
| Reached end of file while parsing | Check for an unclosed brace or another incomplete construct. |
Start with the first reported error. One missing brace or quote can cause several later messages. Fix one thing, run again, and compare the result.
The quick recap
- A class is the outer container in this conventional Java example.
mainis the entry point used by these examples.- Braces group code; semicolons end statements such as method calls.
- Java names are case-sensitive.
- The fastest way to learn the syntax is to change a small example, predict, and run.
To run the starter locally, use javac Main.java, followed by java Main. Need installation help? Return to Getting Started.