Java Syntax

Java Tutorial

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:

PartMeaning
class MainDefines 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:

CorrectIncorrect
System.out.println()system.out.println()
mainMain (won’t run)
classClass

 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

RuleExample
Class name starts with Capital letterclass Student { }
Method and variable names start with lowercaseint age; void display()
CamelCase conventionstudentName, totalAmount

Invalid:

Valid:

 


 Java Print Statements

 

Example:

 

Output:

Hello Java

 Variables and Data Types Example


 Syntax Errors vs. Runtime Errors

TypeMeaningExample
Syntax ErrorCode written incorrectlyMissing semicolon
Runtime ErrorError while program runsDivide by zero
Logic ErrorWrong outputIncorrect 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.

You may also like...