Java
A statically typed, class-based language that compiles to bytecode for the JVM. Portable, safe, and everywhere in back ends.
Level: intermediate
What is it?
Java is a statically typed, object-oriented, garbage-collected language. The javac compiler emits bytecode, and the Java Virtual Machine interprets it and JIT-compiles hot paths, so the same bytecode runs on any platform with a JVM. It ships with a large standard library, built-in threads and concurrency utilities, and a mature ecosystem of build tools and frameworks.
Why does it matter?
Java is the workhorse of large back-end systems and Android, and the JVM hosts Kotlin, Scala, and Clojure too. It is a clear, widely used example of a managed runtime: GC, JIT, class loading, and a strict type system.
Mental model
A shipping container. You pack your program once into bytecode, and any port with a JVM can unload and run it. The container standard, not the cargo, is what makes it portable.
How it works
- Everything lives in a class; single inheritance for classes, multiple interfaces.
- Strong static typing with generics (which are erased at run time), and type inference with var.
- The JVM provides garbage collection, the JIT, security checks, and a memory model for threads.
- Checked exceptions force callers to handle or declare certain failures.
- Records, sealed types, and pattern matching in modern Java reduce boilerplate.
- Threads, executors, and concurrent collections are part of the platform; locks and volatile follow the Java memory model.
Steps
- Write .java source
- javac compiles to bytecode
- Class loader loads .class files
- JVM interprets and JIT-compiles
- GC manages the heap
Code example: A record and a sorted stream
import java.util.*;
public class Main {
record Point(int x, int y) {}
public static void main(String[] args) {
List<Point> pts = List.of(new Point(2, 1), new Point(1, 5));
pts.stream()
.sorted(Comparator.comparingInt(Point::x))
.forEach(System.out::println); // Point[x=1, y=5] then Point[x=2, y=1]
}
}Real-world usage
- Enterprise and cloud back ends
- Android applications
- Big-data and streaming infrastructure
Common mistakes
- Comparing strings or boxed values with == instead of equals().
- Ignoring the memory cost of many small objects.
- Sharing mutable state between threads without synchronization.
Interview questions
What is the JVM and why does it matter?
The Java Virtual Machine runs bytecode. It provides portability across operating systems, garbage collection, a JIT compiler, and a defined memory model, so the same compiled code runs anywhere a JVM exists.
== versus equals() in Java?
== compares references for objects (whether they are the same object), while equals() compares logical equality as defined by the class. Use equals() for strings and value objects.
What does type erasure mean for generics?
Generic type parameters are checked at compile time and erased in the bytecode, so List<String> and List<Integer> are the same class at run time. That is why you cannot do new T[] or instanceof List<String>.