Author
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.
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.
Here’s a simple example:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
| 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 ;. |
Case Sensitivity:
Java is case-sensitive.
→ MyClass and myclass are 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 → File HelloWorld.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 */
| 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 |
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();
}
}
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 userExample (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!");}}
| 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 |