Java syntax refers to the rules used to write valid Java programs. These rules define how we write classes, methods, statements, variables, and expressions.
Every basic Java program can contain a class and a main method.
public class Hello {
public static void main(String[] args) {
System.out.println("Hello Java");
}
}
Output:
Hello Java
public class Hello declares a class.main() is the application's entry point.System.out.println() displays output.A class is a blueprint used to define objects. It can contain variables, constructors, and methods.
public class Student {
String name;
int age;
}
In this example:
class is a Java keyword.Student is the class name.name and age are instance variables.public,
its filename must match the class name. For example,
save this class in Student.java.
The main method is the standard starting point for launching a simple Java application.
public static void main(String[] args) {
System.out.println("Welcome");
}
public — makes the method accessible.static — allows invocation without creating an instance of the class.void — indicates that the method returns no value.main — the method name recognized by the Java launcher.String[] args — stores command-line arguments.A statement is an instruction that tells Java to perform an operation.
int age = 20;
System.out.println(age);
age = 25;
System.out.println(age);
Output:
20
25
The statements declare a variable, display its value, update the variable, and display the updated value.
Most Java statements end with a semicolon
(;).
int number = 10;
System.out.println(number);
Output:
10
Incorrect example:
int number = 10
System.out.println(number);
The first statement is missing a semicolon, so the compiler reports a syntax error.
Curly braces { } group statements into
blocks. They are commonly used with classes, methods,
loops, and conditional statements.
public class Example {
public static void main(String[] args) {
if (10 > 5) {
System.out.println("Ten is greater than five");
}
}
}
Output:
Ten is greater than five
Make sure every opening brace has a matching closing brace.
Java distinguishes between uppercase and lowercase letters.
int age = 20;
int Age = 30;
System.out.println(age);
System.out.println(Age);
Output:
20
30
Here, age and Age are two
different variables.
public is valid, but Public
is not the same keyword.
Identifiers are names given to classes, variables, methods, and other program elements.
Valid identifiers:
studentName
age
total_marks
number1
_myVariable
Invalid identifiers:
2number
student-name
class
Keywords are reserved words with special meanings in Java.
| Keyword | Purpose |
|---|---|
class |
Declares a class. |
int |
Declares an integer variable. |
if |
Starts a conditional statement. |
else |
Defines an alternative conditional branch. |
static |
Declares a class-level member. |
return |
Returns from a method and may provide a value. |
Keywords have predefined meanings and cannot be used as ordinary variable or class names.
Comments explain code and are ignored by the compiler as executable instructions.
Single-line comment:
// This is a single-line comment
System.out.println("Hello Java");
Multi-line comment:
/*
This is a multi-line comment.
It can span several lines.
*/
System.out.println("Java Programming");
Documentation comment:
/**
* Displays a welcome message.
*/
public static void welcome() {
System.out.println("Welcome");
}
Naming conventions help make Java code easier to read and maintain.
| Element | Common Convention | Example |
|---|---|---|
| Class | PascalCase | StudentDetails |
| Variable | camelCase | studentName |
| Method | camelCase | calculateTotal() |
| Constant | Uppercase with underscores | MAX_VALUE |
These are conventions, not strict syntax rules. Following them improves readability.
Syntax errors occur when source code violates Java's language rules.
Incorrect code:
public class Demo {
public static void main(String[] args) {
System.out.println("Hello")
}
}
The print statement is missing a semicolon.
Correct code:
public class Demo {
public static void main(String[] args) {
System.out.println("Hello");
}
}
Output:
Hello
Read compiler messages carefully to find the location and type of an error.
Create a Java program that declares a name and age, then displays both values.
public class Student {
public static void main(String[] args) {
String studentName = "Rahul";
int age = 20;
System.out.println("Name: " + studentName);
System.out.println("Age: " + age);
}
}
Output:
Name: Rahul
Age: 20
Key Points:
1. Which symbol usually ends a Java statement?
2. Is Java case-sensitive?
3. Which is a valid Java identifier?