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.

1️⃣ Single-line Comment



 

2️⃣ Multi-line Comment



 

3️⃣ 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

 


πŸ“Œ Java Print Statements



 

Output:

Hello Java

πŸ“Œ 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.

CodeCapsule

Sanjit Sinha β€” Web Developer | PHP β€’ Laravel β€’ CodeIgniter β€’ MySQL β€’ Bootstrap Founder, CodeCapsule β€” Student projects & practical coding guides. Email: info@codecapsule.in β€’ Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *