Skip to main content

Core APIs Overview

Java's core utility classes live in java.lang and java.util and appear in virtually every codebase. Object defines the root contract; String handles immutable text; StringBuilder and StringJoiner handle mutable construction; the math and wrapper classes bridge numerics and the object system; Optional makes optional values explicit. Mastering these prevents a category of subtle, hard-to-trace bugs.

Key Concepts at a Glance

  • Object: Root class of all Java classes. Defines equals, hashCode, toString, clone, wait/notify. The equalshashCode contract is the most critical: if a.equals(b) then a.hashCode() must equal b.hashCode().
  • String immutability: A String's content never changes after creation. Every "modification" method returns a new object.
  • String pool: JVM interns string literals into a shared heap area. Two "hello" literals are the same object. new String("hello") bypasses the pool.
  • StringBuilder: Mutable, resizable character buffer. Use inside loops instead of + concatenation to avoid O(n²) object creation.
  • StringJoiner: Builds delimited strings with a prefix and suffix. The engine behind String.join and Collectors.joining. Eliminates trailing-delimiter bugs.
  • Wrapper classes: Integer, Long, Double etc. box primitives into objects for collections and generics. Autoboxing/unboxing is done by the compiler transparently.
  • Integer cache: Integer.valueOf(n) caches -128 to 127. == on Integer only works reliably in that range — always use .equals().
  • Math / StrictMath: Static mathematical utilities. The *Exact family (addExact, multiplyExact, toIntExact) throws ArithmeticException on overflow instead of silently wrapping.
  • Optional<T>: A container that holds a value or is empty. Intended exclusively as a method return type — not fields, parameters, or DTOs.
  • orElse vs orElseGet: orElse evaluates its argument eagerly; orElseGet is lazy. Use orElseGet for expensive defaults.
  • Autoboxing NPE: Unboxing a null wrapper (Integerint) throws NullPointerException silently at the assignment site.

Quick-Reference Table

API / MethodPurposeKey Note
Objects.equals(a, b)Null-safe equalityAlways use instead of a.equals(b) when a might be null
Objects.hash(f1, f2, ...)Combine fields for hashCodeMust use the same fields as equals
String.equals(s)Value equalityNever use == for String comparison
"lit".equals(var)NPE-safe comparisonPut literal on the left — returns false if var is null
String.strip()Whitespace removalJava 11+; use over trim() for Unicode awareness
String.isBlank()Empty-or-whitespace checkJava 11+; isEmpty() misses whitespace-only strings
String.join(delim, ...)Static joinConvenience for StringJoiner
Collectors.joining(delim, pre, suf)Stream joiningBacked by StringJoiner
StringBuilder.append(x)Mutable buildReturns this for chaining
Integer.parseInt(s)StringintThrows NumberFormatException on invalid input
Integer.compare(a, b)Safe comparatorAvoids subtraction overflow
Integer.MAX_VALUE / MIN_VALUEBoundary constants-2,147,483,648 to 2,147,483,647
Math.addExact(a, b)Overflow-safe additionThrows ArithmeticException on overflow
Math.abs(Integer.MIN_VALUE)⚠ Overflow trapReturns MIN_VALUE — use Math.absExact (Java 15+)
Math.hypot(a, b)√(a²+b²) without overflowPrefer over manual sqrt(a*a+b*b)
Optional.ofNullable(v)Safe Optional creationReturns empty if v is null
optional.orElseGet(supplier)Lazy defaultCall only when Optional is empty
optional.map(fn)Transform if presentReturns Optional<R>
optional.flatMap(fn)Transform returning OptionalAvoids Optional<Optional<T>> nesting
optional.orElseThrow(supplier)Throw on emptyIdiomatic failure signal

Learning Path

Suggested reading order for a returning Java developer:

  1. Object Class — Start here. The equals/hashCode contract is foundational to every collection; violating it causes silent data loss.
  2. String, StringBuilder, StringJoiner — Covers daily text manipulation; the immutability model and +-in-loops performance trap are interview staples.
  3. Wrapper Classes — Covers the Integer cache, autoboxing NPE, and Integer.compare — critical before studying collections.
  4. Math & StrictMath — Overflow-safe arithmetic and rounding; most important for numeric business logic.
  5. Optional — Learn the good patterns (map/flatMap pipeline, orElseGet) then the anti-patterns (fields, parameters, DTOs).

Top 5 Interview Questions

Q1: What is the equalshashCode contract and what happens when it is violated? A: If two objects are equal under equals, they must return the same hashCode. Violating this causes HashMap and HashSet to look in the wrong bucket, so logically equal keys appear as absent — map.get(equivalentKey) returns null. This is a silent data loss bug that is very hard to diagnose.

Q2: Why is String immutable and what is the string pool? A: Immutability enables the string pool (JVM can reuse the same object for identical literals without risk), makes strings thread-safe without locks, and makes them safe as HashMap keys. The pool stores interned literals; new String("hello") bypasses it, creating a separate heap object — which is why == between a literal and a new String(...) returns false.

Q3: What is the Integer cache trap? A: Integer.valueOf(n) returns the same cached object for -128 to 127. For values outside that range, a fresh object is created on each call. This means Integer a = 200; Integer b = 200; a == b is false, while the same code with 100 is true. Always use equals() for value comparison of wrapper objects.

Q4: When should you use orElseGet instead of orElse on an Optional? A: Use orElseGet(supplier) whenever the default value is expensive to compute (involves I/O, DB access, complex object construction). orElse(default) evaluates the default eagerly even when the Optional has a value. orElseGet only calls the supplier when the Optional is empty, making it lazy and O(1) when a value is present.

Q5: What is Math.addExact and why does it exist? A: Math.addExact(a, b) throws ArithmeticException when the result overflows int (or long). Standard + silently wraps around — Integer.MAX_VALUE + 1 = Integer.MIN_VALUE — which is almost always a logic error in business code. addExact (and multiplyExact, subtractExact, toIntExact) detect overflow explicitly so the error surfaces immediately rather than producing a corrupt silent result.


All Notes in This Domain

NoteDescription
Object Classequals, hashCode, toString, clone, wait/notify — the root contract every Java object honours.
String, StringBuilder, StringJoinerImmutable text, string pool, StringBuilder buffer, StringJoiner delimiter assembly.
Math & StrictMathMathematical utilities, overflow-safe *Exact methods, rounding modes, trig.
Wrapper ClassesInteger/Long/Double boxing, Integer cache, autoboxing NPE, parseInt, compare.
OptionalNullable container for method return types; map/flatMap pipeline; anti-patterns.