Java Basics

Section 1: Java Basics

Lesson 1: Java Syntax and Structure

1.1 Understanding Java's Syntax Rules

Java's syntax follows a set of rules that dictate how programs should be written. Key syntax rules include:


Example:

public class HelloWorld {

    public static void main(String[] args) {

        System.out.println("Hello, World!");

    }

}


1.2 Basic Structure of a Java Program

A basic Java program consists of a class definition, a main method, and statements inside the method. Here's the structure:

public class ClassName {

    public static void main(String[] args) {

        // Your code goes here

    }

}


Lesson 2: Variables and Data Types

2.1 Declaring and Initializing Variables

Variables are containers for storing data. In Java, you need to declare the variable type before using it.

Example:

public class VariablesExample {

    public static void main(String[] args) {

        // Declaration

        int age;

        

        // Initialization

        age = 25;


        // Combined declaration and initialization

        double salary = 50000.5;


        // Output

        System.out.println("Age: " + age);

        System.out.println("Salary: " + salary);

    }

}


2.2 Different Data Types in Java (int, float, double, char)

Java supports various data types, including:

Example: 

public class DataTypesExample {

    public static void main(String[] args) {

        int age = 25;

        float price = 10.99f;

        double salary = 50000.5;

        char grade = 'A';


        System.out.println("Age: " + age);

        System.out.println("Price: " + price);

        System.out.println("Salary: " + salary);

        System.out.println("Grade: " + grade);

    }

}


Lesson 3: Control Flow and Decision Making

3.1 Conditional Statements (if, else if, else)

Conditional statements in Java allow you to execute different blocks of code based on certain conditions.

Example: 

public class ConditionalExample {

    public static void main(String[] args) {

        int age = 20;


        if (age < 18) {

            System.out.println("You are a minor.");

        } else if (age >= 18 && age < 60) {

            System.out.println("You are an adult.");

        } else {

            System.out.println("You are a senior citizen.");

        }

    }

}


3.2 Switch Statements

Switch statements provide a way to handle multiple conditions more efficiently.

Example: 

public class SwitchExample {

    public static void main(String[] args) {

        int dayOfWeek = 2;


        switch (dayOfWeek) {

            case 1:

                System.out.println("Monday");

                break;

            case 2:

                System.out.println("Tuesday");

                break;

            // Add cases for other days

            default:

                System.out.println("Invalid day");

        }

    }

}


Lesson 4: Loops and Iteration

4.1 For Loops, While Loops

Loops allow you to execute a block of code repeatedly. Java supports for and while loops.

Example (for loop): 

public class ForLoopExample {

    public static void main(String[] args) {

        for (int i = 1; i <= 5; i++) {

            System.out.println("Iteration " + i);

        }

    }

}

Example (while loop): 

public class WhileLoopExample {

    public static void main(String[] args) {

        int i = 1;

        while (i <= 5) {

            System.out.println("Iteration " + i);

            i++;

        }

    }

}


4.2 Loop Control Statements (break, continue)

Loop control statements help manage the flow of loops. break terminates a loop, and continue skips the rest of the loop and moves to the next iteration.

Example: 

public class LoopControlExample {

    public static void main(String[] args) {

        for (int i = 1; i <= 5; i++) {

            if (i == 3) {

                // Skip iteration 3

                continue;

            }

            System.out.println("Iteration " + i);

            if (i == 4) {

                // Terminate the loop at iteration 4

                break;

            }

        }

    }

}

With these Java basics, you have a solid foundation for further exploration of the language. Experiment with the code examples, modify them, and observe the results to deepen your understanding.