1. Introduction & JVM

Java is a statically typed and object-oriented programming language that is extremely popular. Its main strength is 'Write Once, Run Anywhere' (WORA) thanks to the Java Virtual Machine (JVM).

The JVM acts as an abstraction layer between your Java code and the operating system, allowing the same `.class` file to run on Windows, Mac, and Linux.

// 'main' is the entry point for all Java programs. public class Main { public static void main(String[] args) { System.out.println("Hello, Java!"); } }

2. Primitive vs. Object Data Types

Java has two categories of data types: Primitive and Object (Reference).

1. Primitives: Stored directly in memory (stack). Examples: `int` (whole number), `double` (decimal), `char` (character), `boolean` (true/false).

2. Objects: Stored in the 'heap' and referenced by a pointer. Examples: `String`, `ArrayList`, or any class you create.

// Primitive Types int age = 25; double pi = 3.14; boolean isJavaFun = true; // Object (Reference) Types String greeting = "Hello World"; // 'Integer' is the Wrapper Class for 'int' Integer totalScore = 100;

3. Control Flow

Java's control structures are similar to C-based languages. You use `if-else` for conditionals, and `for`, `while`, and `do-while` for looping.

int score = 85; if (score > 90) { System.out.println("A"); } else if (score > 80) { System.out.println("B"); } else { System.out.println("C"); } // Standard 'for' loop for (int i = 0; i < 3; i++) { System.out.println(i); } // 'while' loop int count = 0; while (count < 2) { System.out.println("Counting..."); count++; }

4. OOP: Classes & Objects

Java is an OOP-centric language. A Class is the 'blueprint'. An Object is the 'instance' or the actual product built from that blueprint.

A Class consists of fields (variables/data) and methods (functions/behavior).

// 1. Defining the Class (Blueprint) public class Car { // Fields (Data) String color; // Constructor (Special method when object is created) public Car(String color) { this.color = color; } // Method (Behavior) public void startEngine() { System.out.println("Engine started!"); } } // 2. Creating an Object (Instance) Car myCar = new Car("Red"); myCar.startEngine(); // Calling the method

5. OOP: Encapsulation & Inheritance

Encapsulation is the practice of hiding data (fields) using `private` and only exposing them through 'getter' and 'setter' methods.

Inheritance allows one class (child) to inherit fields and methods from another class (parent) using the `extends` keyword.

// Parent Class class Vehicle { protected String brand = "Tesla"; // protected can be accessed by child } // Child Class (inherits from Vehicle) class ElectricCar extends Vehicle { private int batteryLevel = 100; // Getter for 'private' data public int getBatteryLevel() { return this.batteryLevel; } } ElectricCar modelS = new ElectricCar(); System.out.println(modelS.brand); // "Tesla" (from parent) System.out.println(modelS.getBatteryLevel()); // 100 (from child)

6. Java Collections Framework

Java provides a powerful framework for managing collections of data. The three most important interfaces are:

1. List: An ordered collection, allows duplicates. (Common implementation: `ArrayList`).

2. Set: An unordered collection, does NOT allow duplicates. (Common implementation: `HashSet`).

3. Map: A collection of Key-Value pairs. (Common implementation: `HashMap`).

import java.util.List; import java.util.ArrayList; import java.util.Map; import java.util.HashMap; // List List<String> names = new ArrayList<>(); names.add("Alice"); names.add("Bob"); // Map Map<String, Integer> scores = new HashMap<>(); scores.put("Alice", 90); scores.put("Bob", 85);

7. Exception Handling

When an error occurs (e.g., trying to read a file that doesn't exist), Java will 'throw' an Exception. We handle this using a `try-catch-finally` block.

try { // Code that might fail int result = 10 / 0; // This will throw an ArithmeticException } catch (ArithmeticException e) { // Handle the error here System.out.println("Error: Cannot divide by zero!"); } finally { // This code ALWAYS runs System.out.println("Process finished."); }

Practice Set

Reinforce basic Java concepts, from syntax, OOP, to Collections.

Question #1 - Java Introduction

What does JVM stand for, which allows Java to run on any operating system?

Question #2 - Data Types

What is the main difference between `int` and `Integer` in Java?

Question #3 - Variables

What keyword is used to declare a variable whose value CANNOT be changed (a constant)?

Question #4 - OOP

What keyword is used to inherit properties from one Class to another?

Question #5 - OOP

The special method in a Class that has the SAME name as the Class and is executed when an object is created is called?

Question #6 - Access Modifier

Which access modifier makes a property or method accessible ONLY FROM WITHIN the class itself?

Question #7 - Collections

Which collection data structure is most efficient for storing unique data (without duplicates)?

Question #8 - Exception Handling

What code block is ALWAYS executed after a `try` block (whether an error occurred or not)?

Final Exam

Comprehensively test your Java knowledge, including OOP concepts and Exception handling.

Question #1 - Java Introduction

What does JVM stand for, which allows Java to run on any operating system?

Question #2 - Data Types

What is the main difference between `int` and `Integer` in Java?

Question #3 - Variables

What keyword is used to declare a variable whose value CANNOT be changed (a constant)?

Question #4 - OOP

What keyword is used to inherit properties from one Class to another?

Question #5 - OOP

The special method in a Class that has the SAME name as the Class and is executed when an object is created is called?

Question #6 - Access Modifier

Which access modifier makes a property or method accessible ONLY FROM WITHIN the class itself?

Question #7 - Collections

Which collection data structure is most efficient for storing unique data (without duplicates)?

Question #8 - Exception Handling

What code block is ALWAYS executed after a `try` block (whether an error occurred or not)?

Question #9 - OOP

What is meant by 'Polymorphism'?

Question #10 - Static

What is the meaning of the `static` keyword on a method or variable?