Java, like any programming language, has its own set of rules for how code must be written. These rules, known as syntax, determine the structure and formatting of your code, ensuring it's understandable for both humans and the Java compiler. Here's a breakdown of some key Java syntax rules:
- 1. Case Sensitivity: Java is case-sensitive. This means that
MyVariableis different frommyvariable, and they are treated as distinct entities.
String MyVariable = "Hello";
String myvariable = "World";-
2. Identifiers: Names given to variables, classes, methods, and other code elements are called identifiers. These identifiers must follow these rules:
-
Start with a letter (A-Z, a-z) or an underscore (_).
-
Can contain letters, numbers, and underscores after the first character.
-
Cannot be reserved keywords (e.g.,
int,if,else). -
3. Comments: Comments are lines of text that are ignored by the compiler but provide explanations and notes for human readers. There are two types:
-
Single-line comments: Begin with
//and extend to the end of the line. -
Multi-line comments: Enclosed within
/*and*/.
// This is a single-line comment explaining the purpose of the code
/*
This is a multi-line comment describing the logic behind the code block
in your code!
*/-
4. Semicolons: Every statement in Java (except for class declarations and certain control flow statements) must end with a semicolon
;. -
5. Braces: Braces
{}are used to group statements together and define the scope of code blocks. For example, the body of a function or loop is enclosed within braces. -
6. Data Types: Variables must be declared with a specific data type, which specifies the type of data they can hold (e.g.,
intfor integers,doublefor floating-point numbers,Stringfor text).
// Integer variable to store age
int age = 25;
// Double variable for decimal value
double pi = 3.14159;
// String variable to store name
String name = "John Doe";- 7. Operators: Java provides various operators for performing operations on data. These include arithmetic operators (+, -, *, /), comparison operators (==, !=, <, >), logical operators (&&, ||, !), and more.
// Variables
int number1 = 5;
int number2 = 10;
// Maths
int result1 = number1 + number2;
int result2 = number1 - number2;
int result3 = number1 * number2;
int result4 = number1 / number2;
// Comparison
if (number1 == 5) {}; // Return True
if (number1 != number2) {}; // Return True
if (number1 > number2) {}; // Return False
if (number1 < number2) {}; // Return True- 8. Control Flow Statements: These statements control the flow of execution in your program. Examples include conditional statements (if, else, switch), and looping statements (for, while, do-while).
// (if-else)
int number = 10;
if (number > 0) {
System.out.println("The number is positive.");
} else {
System.out.println("The number is non-positive.");
}// (for loop)
for (int i = 0; i < 5; i++) {
System.out.println("Number " + (i + 1));
}-
9. Code Indentation: While not strictly enforced by the compiler, proper indentation is strongly recommended for readability and maintainability. Indentation visually represents code blocks and improves the overall structure of your program.
-
10. Access Modifiers: These keywords (public, private, protected) control the visibility and access of code elements within your program.
By understanding and adhering to these syntax rules, you can write well-structured, readable, and maintainable Java code. As you progress further, you'll encounter more specific rules and best practices related to different programming constructs.