1. Introduction to Java Syntax

Java syntax refers to the set of rules that govern how programs are written in the Java programming language. It’s crucial to understand these rules to write correct and meaningful code.

Java programs are composed of classes, and each class contains methods that define the program’s behavior. The entry point of a Java program is the main method, which has the following syntax:

public static void main(String[] args) {
    // Code to be executed
}

2. Variables and Data Types

Variables are used to store data in a Java program. Each variable has a data type that defines the type of value it can hold. Java has primitive data types (e.g., int, double, boolean) and reference data types (e.g., String, custom classes).

int age = 25;
double pi = 3.14159;
boolean isStudent = true;
String name = "Alice";

3. Control Structures

Control structures enable you to make decisions and repeat actions in your program.

Conditional Statements (if-else)

int num = 10;
if (num > 0) {
    System.out.println("Number is positive");
} else {
    System.out.println("Number is non-positive");
}
Number is positive

Loops (for, while)

for (int i = 0; i < 5; i++) {
    System.out.println("Iteration: " + i);
}

int count = 0;
while (count < 3) {
    System.out.println("Count: " + count);
    count++;
}
Iteration: 0
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Count: 0
Count: 1
Count: 2

4. Functions (Methods)

Functions, or methods, are blocks of code that perform a specific task. They help in modularizing code and promoting reusability.

// Method declaration
public static int add(int a, int b) {
    return a + b;
}

// Method call
int result = add(5, 3);
System.out.println("Result: " + result);
Result: 8

5. Object-Oriented Concepts

Java is an object-oriented programming language. It uses classes and objects to structure code.

Class Definition

class Circle {
    double radius;

    double calculateArea() {
        return 3.14159 * radius * radius;
    }
}

Object Creation and Usage

Circle myCircle = new Circle();
myCircle.radius = 5.0;
double area = myCircle.calculateArea();
System.out.println("Area: " + area);
Area: 78.53975