Fundamentals 36 min read

Java Fundamentals: OOP Concepts, JVM/JDK/JRE, and Common Interview Topics

This article provides a comprehensive overview of core Java concepts for interview preparation, covering object‑oriented programming fundamentals, differences between OOP and procedural programming, detailed explanations of JVM, JDK, JRE, comparisons with C++, string handling, constructors, inheritance, polymorphism, threading, garbage collection, and related code examples.

Architecture Digest
Architecture Digest
Architecture Digest
Java Fundamentals: OOP Concepts, JVM/JDK/JRE, and Common Interview Topics

Java Fundamentals for Interview Preparation

The article gathers essential Java knowledge useful for interview review, covering language basics, runtime components, and common pitfalls.

Object‑Oriented Programming (OOP)

Procedural programming treats a program as a sequence of commands (functions). OOP treats a program as a collection of objects that exchange messages.

In a nutshell, OOP is encapsulation, inheritance, and polymorphism.

Encapsulation

Hides internal implementation details behind a well‑defined interface.

Reduces coupling and allows internal changes without affecting callers.

Improves readability, maintainability, and security.

Inheritance

Establishes an is‑a relationship; a subclass is a more specific version of its superclass.

Polymorphism

Enables a single interface to have multiple implementations; the actual method executed is determined at runtime.

Procedural vs. Object‑Oriented

Procedural code often yields higher performance (e.g., in embedded systems) but lacks the maintainability, reuse, and extensibility of OOP.

JVM, JDK, and JRE

The Java Virtual Machine (JVM) executes Java bytecode. Bytecode ( .class files) is platform‑independent, enabling "write once, run anywhere".

In Java, the code the JVM can understand is called bytecode (files with the .class extension). It is not tied to any specific processor but to the virtual machine.

Typical compilation steps: source → bytecode → (optional) JIT compilation → machine code.

JDK vs. JRE

The JDK (Java Development Kit) includes the JRE plus compilers, debuggers, and other development tools. The JRE provides only the libraries and JVM needed to run Java programs.

Java vs. C++

Both are object‑oriented and support encapsulation, inheritance, and polymorphism.

Java lacks direct pointer manipulation, offering safer memory handling.

Java supports only single class inheritance (multiple inheritance via interfaces), whereas C++ allows multiple class inheritance.

Java provides automatic memory management; C++ requires manual deallocation.

String, StringBuilder, and StringBuffer

Immutability : String stores characters in a private final char[] value , making it immutable. StringBuilder and StringBuffer use a mutable char[] value without the final modifier.

Thread safety : String is inherently thread‑safe. StringBuffer adds synchronized methods, while StringBuilder is not synchronized.

Performance : Modifying a String creates a new object each time; StringBuilder modifies the existing buffer, offering 10‑15% faster performance in single‑threaded scenarios.

abstract class AbstractStringBuilder implements Appendable, CharSequence {
    char[] value;
    int count;
    AbstractStringBuilder() {}
    AbstractStringBuilder(int capacity) {
        value = new char[capacity];
    }
}

Constructors, Overloading, and Overriding

Constructors cannot be overridden because they are not inherited; they can only be overloaded (multiple constructors with different signatures).

Method Overloading vs. Overriding

Overloading : Same method name, different parameter lists, resolved at compile time.

Overriding : Same signature in a subclass, resolved at runtime; return type must be compatible, and access level cannot be more restrictive.

Thread Lifecycle in Java

Threads transition through six states: NEW, RUNNABLE (ready/running), BLOCKED, WAITING, TIMED_WAITING, and TERMINATED. The JVM hides the distinction between RUNNABLE and RUNNING; both are reported as RUNNABLE by the OS.

public class test1 {
    public static void main(String[] args) {
        String a = new String("ab"); // a is a reference
        String b = new String("ab"); // b is another reference, same content
        String aa = "ab"; // from string pool
        String bb = "ab"; // from string pool
        if (aa == bb) System.out.println("aa==bb"); // true
        if (a == b) System.out.println("a==b"); // false
        if (a.equals(b)) System.out.println("aEQb"); // true
    }
}

hashCode and equals

Contract: equal objects must have identical hash codes; unequal objects may share hash codes. Overriding equals() requires also overriding hashCode() to maintain collection correctness (e.g., in HashSet ).

Garbage Collection (GC)

GC automatically reclaims memory occupied by unreachable objects. Common algorithms:

Reference Counting : Simple but cannot collect cyclic references.

Mark‑Sweep : Traverses reachable objects from GC roots, then frees unmarked objects (may cause fragmentation).

Mark‑Compact : Like mark‑sweep but also compacts live objects to eliminate fragmentation.

Copying (Stop‑and‑Copy) : Divides the heap into two halves; live objects are copied to the empty half, providing allocation speed and eliminating fragmentation.

Generational Collectors : Separate young and old generations; young generation is collected frequently (Minor GC), old generation less often (Major/Full GC).

Young generation layout: one Eden space and two Survivor spaces (S0, S1) typically in an 8:1:1 ratio. Objects that survive several Minor GCs are promoted to the old generation.

Common Collectors

Serial (single‑threaded, copying for young gen, mark‑compact for old gen)

Parallel Scavenge (multi‑threaded, high throughput for young gen)

CMS (Concurrent Mark‑Sweep, low pause times for old gen)

G1 (region‑based, balances pause time and throughput)

Memory Leaks in Java

Even with GC, leaks can occur when objects remain reachable unintentionally, such as static collections holding references:

Static Vector v = new Vector();
for (int i = 1; i < 100; i++) {
    Object o = new Object();
    v.add(o);
    o = null;
}

Other leak sources include unclosed I/O, database, or network connections, and listeners that are not deregistered.

References

https://stackoverflow.com/questions/1906445/what-is-the-difference-between-jdk-and-jre

https://www.educba.com/oracle-vs-openjdk/

https://stackoverflow.com/questions/22358071/differences-between-oracle-jdk-and-openjdk?answertab=active#tab-top

JavaJVMgarbage collectionJDKinterviewoopthreadingJRE
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.