Java Syntax

Java Syntax (Beginner-Friendly Guide)
Once Java is installed and ready, the next step is understanding the syntax — the rules for writing Java code correctly.
Basic Java Program Structure
Breakdown:
| Part | Meaning |
|---|---|
class Main | Defines a class named Main |
{ } | Curly braces define the body of the class or method |
public static void main(String[] args) | Entry point of every Java program |
System.out.println() | Prints output to the console |
Java is Case Sensitive
Examples:
| Correct | Incorrect |
|---|---|
System.out.println() | system.out.println() |
main | Main (won’t run) |
class | Class |
Statements and Semicolons
Every executable statement ends with a semicolon (;).
Example:
Comments in Java
Comments are ignored by the compiler.
Single-line Comment
Multi-line Comment
Documentation Comment (For APIs)
Java Naming Rules
| Rule | Example |
|---|---|
| Class name starts with Capital letter | class Student { } |
| Method and variable names start with lowercase | int age; void display() |
| CamelCase convention | studentName, totalAmount |
Invalid:
1 2 | int 1count; class public; (keyword not allowed) |
Valid:
1 2 | int count1; int _value; |
Java Print Statements
Example:
1 2 | System.out.print("Hello "); System.out.print("Java"); |
Output:
Variables and Data Types Example
Syntax Errors vs. Runtime Errors
| Type | Meaning | Example |
|---|---|---|
| Syntax Error | Code written incorrectly | Missing semicolon |
| Runtime Error | Error while program runs | Divide by zero |
| Logic Error | Wrong output | Incorrect formula |
Example Program with Variables & Comments
Key Points to Remember
Java code must be inside a class.
Program execution always starts from
main()method.Statements end with
;Java uses {} to group code blocks.
Follow camelCase naming rules.
