Learn Guide

Java Fundamentals

OOP, collections, and the JVM ecosystem explained.

What is Java?

Java is a class-based, object-oriented language designed for portability — "write once, run anywhere" via the JVM (Java Virtual Machine). Widely used in enterprise backends (Spring), Android, and large-scale distributed systems.

Types & Variables

int     count  = 10;
double  price  = 9.99;
boolean active = true;
String  name   = "Alice";   // String is an object, not a primitive
var     x      = 42;        // local type inference (Java 10+)

Classes & OOP

public class Animal {
    private String name;
    public Animal(String name) { this.name = name; }
    public String getName()    { return name; }
    public String speak()      { return "..."; }
}

public class Dog extends Animal {
    public Dog(String name) { super(name); }

    @Override
    public String speak() { return "Woof!"; }
}

Interfaces

public interface Drawable {
    void draw();                              // implicitly abstract
    default String describe() { return "shape"; } // default method
}

public class Circle implements Drawable {
    public void draw() { System.out.println("Circle"); }
}

Collections

List<String>         list = new ArrayList<>();
Set<Integer>         set  = new HashSet<>();
Map<String, Integer> map  = new HashMap<>();

list.add("hello");
map.put("score", 42);
int val = map.getOrDefault("score", 0); // 42

Stream API & Lambdas

List<Integer> nums = List.of(1, 2, 3, 4, 5);

List<Integer> evens = nums.stream()
    .filter(n -> n % 2 == 0)
    .collect(Collectors.toList());  // [2, 4]

int sum = nums.stream()
    .mapToInt(Integer::intValue)
    .sum();                         // 15

Exception Handling

try {
    int result = 10 / 0;
} catch (ArithmeticException e) {
    System.out.println("Error: " + e.getMessage());
} finally {
    System.out.println("always runs");
}

Java vs C# Quick Reference

Java C#
extends : (inheritance)
implements : (interface)
final sealed / readonly
ArrayList<T> List<T>
Getters/Setters { get; set; }