Java Syntax
3 min read ·
Structure of a Java Program
Every Java program follows a fixed structure. Even a simple program has a class and a main method.
Java code execution always starts from the
main method.Note
The class name must match the file name. If the file name is Main.java, the class name must be Main.
Class Declaration
In Java, everything is written inside a class.
A class is a blueprint that defines how an object will behave.
The main Method
The
main method is the entry point of every Java program.Without the main method, the Java program will not run.
Real World Scenario
Think of the main method as a power switch. The program turns on only when this switch exists.
Statements in Java
Each instruction in Java is called a statement. Every statement ends with a semicolon.
The semicolon tells Java that the instruction is complete.
Caution
Missing a semicolon is one of the most common beginner mistakes in Java.
Java Is Case Sensitive
Java treats uppercase and lowercase letters as different.
Both variables are different and stored separately.
Comments in Java
Comments are used to explain code and are ignored by the compiler.
Comments make code easier to read and understand.
Curly Braces and Code Blocks
Java uses curly braces to define blocks of code.
Everything inside the braces belongs to that block.
Whitespace and Formatting
Java ignores extra spaces and empty lines, but proper formatting improves readability.
Well formatted code is easier to maintain and debug.
Pro Tip
Always format your code neatly. Clean code reflects clear thinking.
Java Keywords
Keywords are reserved words with predefined meanings in Java.
Examples include:
class
public
static
void
int
These words cannot be used as variable names.
Stop
Using Java keywords as identifiers will cause compilation errors.
Syntax Errors
Syntax errors occur when Java rules are broken.
Common causes include:
Missing semicolons
Incorrect brackets
Wrong capitalization
Misspelled keywords
The compiler will show an error message pointing to the issue.
Goal Achieved
You now understand the basic syntax rules that every Java program follows.