Java Syntax & Structure – Java Fundamentals
Before diving into object-oriented programming or advanced features, it’s essential to understand Java’s basic syntax and program structure.
This foundation helps you read, write, and organize Java code properly.
What is Java Syntax?
- Syntax means the rules and structure that define how Java code must be written so the compiler can understand it.
- Java syntax is strict but readable, inspired by C and C++.
- Every Java program follows a clear, organized structure consisting of classes, methods, and statements.
Basic Structure of a Java Program
Here’s a simple example:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Breakdown:
| Part | Description |
|---|---|
public class HelloWorld | Defines a class named HelloWorld. The file name must match this class name. |
{ ... } | Curly braces define the body of a class or method. |
public static void main(String[] args) | Entry point of every Java program — where execution starts. |
System.out.println("Welcome to Java!"); | Prints text to the console. Every statement ends with a semicolon ;. |
Key Syntax Rules
-
Case Sensitivity:
Java is case-sensitive.
→
MyClassandmyclassare different identifiers. -
Class Names:
Always start with an uppercase letter.
Example:
Student,BankAccount. -
Method Names:
Use camelCase convention.
Example:
calculateTotal(),getUserName(). -
File Name Rule:
File name must match the public class name.
→ Class
HelloWorld→ FileHelloWorld.java -
Main Method Requirement:
The
main()method is mandatory to execute a Java program.public static void main(String[] args) -
Statements End with Semicolons:
Every executable statement ends with
; -
Comments:
Used for notes or documentation (ignored by compiler).
// Single-line comment /* Multi-line comment */
Common Elements of Java Programs
| Element | Example | Description |
|---|---|---|
| Package Declaration | package myapp; | Organizes related classes |
| Import Statement | import java.util.*; | Includes built-in or external classes |
| Class Declaration | public class MyClass { } | Core building block of Java |
| Main Method | public static void main(String[] args) | Entry point |
| Statements | System.out.println("Hi"); | Instructions to execute |
| Comments | // Example | Non-executable documentation |
Example: Program with Package & Import
package basics;
import java.util.Scanner;
public class InputExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.println("Hello, " + name + "!");
sc.close();
}
}
Here,
package basics;→ Groups the class inside a package named “basics”import java.util.Scanner;→ Imports the Scanner class from Java’s libraryScanner→ Used to take input from the user
Whitespace & Indentation
- Java ignores extra spaces, tabs, and newlines.
- But indentation makes your code more readable and professional.
Example (Good Practice ):
public class Greet {
public static void main(String[] args) {
System.out.println("Hello!");
}
}
Example (Bad Practice ):
public class Greet{public static void main(String[] args){System.out.println("Hello!");}}
Summary
| Concept | Key Point |
|---|---|
| Case-sensitive | Keywords and identifiers must match case |
| Class structure | One public class per file |
| Main method | Execution starts from main() |
| Statements | Must end with ; |
| Comments | Used for notes, ignored by compiler |
| Indentation | Not mandatory but improves readability |
🏆 Top 5 Java Interview Questions - Syntax & Structure
1. What is the structure of a basic Java program?
Answer: Every Java program is built around classes and methods, following a defined structure.
Example:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello Java!");
}
}
Explanation:
public class HelloWorld→ Defines the class (must match file name).{ ... }→ Encloses class and method body.public static void main(String[] args)→ Entry point of the program.System.out.println()→ Prints output to the console.;→ Marks the end of a statement.
2. Why is the main() method important in Java?
Answer: The main() method acts as the entry point for Java applications.
When you run a Java program, the JVM looks for this method to begin execution.
Syntax:
public static void main(String[] args)
Explanation:
public→ Accessible to JVM from anywhere.static→ Can be executed without creating an object.void→ Does not return any value.String[] args→ Accepts command-line arguments.
Without this method, the program cannot execute and throws an error.
3. What are Java naming conventions and why are they important?
Answer: Naming conventions make Java code readable, organized, and standardized across teams.
| Type | Convention | Example |
|---|---|---|
| Class Names | PascalCase | EmployeeData, BankAccount |
| Methods & Variables | camelCase | calculateSalary(), userAge |
| Constants | UPPERCASE | PI, MAX_LIMIT |
Java is case-sensitive → MyClass ≠ myclass.
Following these conventions improves code clarity and reduces errors.
4. What is the rule for class name and file name in Java?
Answer: If a class is declared public, the file name must match the class name exactly (including case).
Example (Correct):
public class Hello {
public static void main(String[] args) {
System.out.println("Hi!");
}
}
File name → Hello.java
Incorrect Example:
public class Hello { } // File name: Test.java → Compilation Error
This rule ensures the compiler can locate and link the correct class.
5. Why are semicolons required in Java statements?
Answer: In Java, the semicolon (;) is used to end executable statements.
It tells the compiler where one instruction ends and the next begins.
Example:
System.out.println("Hello Java!"); // Correct
System.out.println("Welcome") // Missing semicolon → Error
Without semicolons, the compiler cannot parse the code correctly, resulting in a syntax error.
