Introduction to Java Syntax
Java syntax refers to the set of rules that define the structure of a Java program. These rules determine how you write Java code, including classes, methods and statements. Java has a clean and organized structure that is easy to learn for beginners.
Understanding Java syntax is essential because even small syntax errors can prevent your program from running.
Basic Structure of a Java Program
Every Java program follows a specific structure. Here’s a simple example of a basic Java program:
Code Example:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Explanation:
- public class HelloWorld:
- Declares a class named HelloWorld.
- The class name should match the filename (HelloWorld.java).
- public static void main(String[] args):
- The main method is the entry point of a Java program.
- Code inside this method will execute when the program runs.
- System.out.println(“Hello, World!”);:
- This statement prints output to the console.
- System.out.println is a built-in Java method.
Key Rules of Java Syntax
Case Sensitivity:
Java is case-sensitive.
- Example: HelloWorld and helloworld are different.
Class Names:
- The first letter of a class name should be uppercase.
- If the class name has multiple words, use PascalCase.
- Example:
public class MyFirstProgram { }
Method Names:
- Method names start with a lowercase letter.
- Use camelCase for multiple words.
- Example:
public void printMessage() { }
File Name and Class Name:
- The Java file name must match the class name.
- Save your file as ClassName.java.
- Example: If the class is HelloWorld, the file must be HelloWorld.java.
Curly Braces { }:
- Curly braces define the beginning and end of a block of code.
- Example:
public class Example {
public static void main(String[] args) {
System.out.println("Java Syntax");
}
}
Semicolon ; :
- Every statement in Java ends with a semicolon.
- Missing semicolons will cause syntax errors.
Comments:
- Comments are used to explain code and are ignored by the compiler.
- Types of comments in Java:
- Single-line comment: // This is a single-line comment
- Multi-line comment: /* This is a multi-line comment */
- Documentation comment: /** * This is a documentation comment * for generating API docs. */
Java Program Components
Class Declaration:
- A Java program begins with a class declaration.
- The class name must be the same as the file name.
- Example:
public class Example { }
Main Method:
- The main() method is the entry point of every Java program.
- Syntax:
public static void main(String[] args) { }
Statements:
- Code statements are written inside the main method.
- Each statement ends with a semicolon.
- Example:
System.out.println("Welcome to Java!");
Java Variables and Data Types
To store data in Java, variables and data types are used.
Variable Declaration Syntax:
dataType variableName = value;
- dataType: Specifies the type of data (e.g., int, String, boolean).
- variableName: The name of the variable.
Example:
public class VariablesExample {
public static void main(String[] args) {
int number = 10; // Integer variable
String name = "Java"; // String variable
boolean isLearning = true; // Boolean variable
System.out.println("Number: " + number);
System.out.println("Name: " + name);
System.out.println("Learning Java: " + isLearning);
}
}
Output:
Number: 10
Name: Java
Learning Java: true
Java Syntax Example – Multiple Features Combined
Here is a complete program showing multiple syntax rules:
Code Example:
public class StudentDetails {
public static void main(String[] args) {
// Variable declaration
String name = "Alice";
int age = 20;
double marks = 85.5;
// Output details
System.out.println("Student Name: " + name);
System.out.println("Age: " + age);
System.out.println("Marks: " + marks);
}
}
Explanation:
- Variable declaration: Declares variables of type String, int and double.
- Output: Uses System.out.println() to display variable values.
Common Syntax Errors
Missing Semicolon:
System.out.println("Hello World") // Error: Missing semicolon
Mismatched Braces:
public class Example {
public static void main(String[] args) {
System.out.println("Error")
} // Missing closing brace
Case Sensitivity:
System.out.println("Java");
system.out.println("Error"); // Error: System must be uppercase