iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Intro

Java is a mature, statically typed, garbage-collected language. Runs on the JVM, deeply backwards compatible, with enormous library coverage.

Java — what it is

EXAMPLE
// ===== The values =====
// - Stable language; rare breaking changes
// - JVM: write-once, run on Linux/macOS/Windows; profiling + GC tuning are world class
// - Massive library ecosystem (Spring, Jakarta EE, Quarkus, Micronaut)
// - Strong typing + tooling (IntelliJ, Eclipse) makes large codebases tractable

// ===== Hello, world =====
public class Hello {
    public static void main(String[] args) {
        System.out.println("hello, world");
    }
}

// Build + run:
//   javac Hello.java && java Hello
// Modern Java: package via Maven/Gradle; run with mvn exec:java

// ===== Modern Java idioms (17+) =====
record User(int id, String name, String email) {}

var u = new User(1, "Alex", "a@x.io");
System.out.println(u.name());

// Pattern matching for switch (preview/standard in modern releases):
Object obj = 42;
String size = switch (obj) {
    case Integer i when i < 10  -> "small";
    case Integer i when i < 100 -> "medium";
    case Integer i             -> "large";
    default                    -> "non-int";
};

// ===== Streams =====
import java.util.List;
import java.util.stream.Collectors;
List<Integer> evens = List.of(1, 2, 3, 4, 5).stream()
    .filter(n -> n % 2 == 0)
    .collect(Collectors.toList());

// ===== A tiny HTTP server (built-in jdk.httpserver) =====
import com.sun.net.httpserver.HttpServer;
import java.net.InetSocketAddress;
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
server.createContext("/healthz", (exchange) -> {
    var body = "{\"ok\":true}".getBytes();
    exchange.sendResponseHeaders(200, body.length);
    exchange.getResponseBody().write(body);
    exchange.close();
});
server.start();

// ===== When Java wins =====
// - Enterprise systems (banks, telcos, public sector)
// - Long-lived backends with strong SLAs
// - Data systems (Kafka, Cassandra, Elasticsearch, Spark are JVM)
// - Android (still JVM bytecode under the hood)

// ===== When Java hurts =====
// - Short-lived CLIs where JVM startup hurts (use GraalVM native or another language)
// - Tiny dev teams without IDE support comfort
// - Web front-end (no, but yes for backends)

// ===== Patterns to internalise =====
// - Records for value types; sealed interfaces for closed hierarchies
// - var for local type inference where it improves readability
// - Optional<T> over null where it crosses API boundaries
// - try-with-resources for any AutoCloseable

// ===== Pitfalls =====
// - == on String -> compares references; .equals() compares content
// - Unboxing a null Integer -> NullPointerException
// - Using ArrayList where a primitive int[] would do (autobox churn)
// - Catching Exception broadly -> swallow real bugs

Why it matters

Modern Java (17+) reads cleanly: records, var, sealed types, pattern matching. The JVM is one of the best runtimes ever shipped, and the library ecosystem rewards long-lived services. Reach for it when reliability, tooling, and ecosystem depth matter more than language fashion.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
// Java: static typing, OOP, runs on the JVM.
// Source compiles to bytecode (.class), runs anywhere a JVM does.
Try it Yourself »

Exercise

Standard entry-point signature.

public static main(String[] args) { }

Test yourself

Q1. Java compiles source to…
Q2. The entry point is…
Q3. Java memory is managed by…

Discussion

Loading…