Basics

Hello WorldBasic Java program
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
Data typesPrimitive types
int age = 25;
double price = 99.99;
char grade = 'A';
boolean isActive = true;
VariablesVariable declaration
String name = "Mishu";
final int MAX = 100;

Control Flow

If-elseConditional execution
if (score >= 90) {
    System.out.println("A");
} else if (score >= 80) {
    System.out.println("B");
} else {
    System.out.println("C");
}
For loopIteration
for (int i = 0; i < 10; i++) {
    System.out.println(i);
}
While loopCondition-based loop
while (x > 0) {
    x--;
}
Enhanced forIterate over arrays/collections
for (String item : items) {
    System.out.println(item);
}

OOP

ClassBlueprint for objects
public class Car {
    private String model;

    public Car(String model) {
        this.model = model;
    }

    public void drive() {
        System.out.println(model + " is driving");
    }
}
InheritanceExtend a class
public class ElectricCar extends Car {
    public ElectricCar(String model) {
        super(model);
    }
}
InterfaceContract for classes
interface Drawable {
    void draw();
}

class Circle implements Drawable {
    public void draw() { ... }
}
ArrayFixed-size collection
int[] numbers = {1, 2, 3};
int first = numbers[0];
numbers.length;