Comprehensive Java Fundamentals and Interview Guide
This article provides a thorough overview of Java fundamentals for interview preparation, covering JVM/JRE/JDK relationships, bytecode advantages, language features, OOP concepts, exception handling, garbage collection, reference types, and common pitfalls, supplemented with code examples and practical tips to strengthen core Java knowledge.
Java Fundamentals Overview
Java is an object‑oriented language distinguished by its cross‑platform capability (Write once, run anywhere) and automatic garbage collection. Understanding the platform starts with the relationship between JVM, JRE, and JDK.
JVM, JRE, and JDK
JVM Java Virtual Machine is the runtime that executes bytecode on any platform.
JRE Java Runtime Environment bundles the JVM with core class libraries (e.g., java.lang ) needed to run Java programs.
JDK Java Development Kit provides development tools (compiler, jar, etc.) and includes a JRE, so installing JDK alone is sufficient for development.
Execution Model
Java source is compiled by javac into bytecode (.class files). At runtime the JVM interprets bytecode, but modern HotSpot JVMs use a JIT (Just‑In‑Time) compiler to translate hot code paths into native machine code, making execution partially compiled rather than purely interpreted.
Benefits of Bytecode
Bytecode abstracts away hardware specifics, enabling the "once compiled, run anywhere" model. It separates compilation from execution, allowing the same .class files to run on any JVM implementation.
New Features After JDK 1.8
Interface default methods, lambda expressions, functional interfaces, Stream API, method references, the new date‑time API, Optional class, and new tools such as the Nashorn engine and jdeps enhance expressiveness and reduce boilerplate.
Common Interview Topics
JVM, JRE, JDK relationship
Is Java interpreted?
Advantages of bytecode
Features introduced in Java 8
Constructor overloading vs overriding
Difference between wait() and sleep()
Difference between & and &&
Primitive vs wrapper types ( int vs Integer )
OOP fundamentals and four pillars
Exception vs Error hierarchy
Final, finally, finalize distinctions
Strong, soft, weak, and phantom references
String, StringBuilder, StringBuffer usage and performance
HashMap key considerations (using String )
Interface vs abstract class differences
Value vs reference passing semantics
Key Code Examples
Constructor example using this :
public Person(String name, int age) {
this.name = name;
this.age = age;
}Constructor chaining with this() and super() :
class Person {
private String name;
private int age;
public Person() {}
public Person(String name) { this.name = name; }
public Person(String name, int age) {
this(name);
this.age = age;
}
}
class Student extends Person {
private String name;
public Student(String name, String name1) {
super(name);
this.name = name1;
}
public void getInfo() {
System.out.println(this.name); // child
System.out.println(super.name); // parent
}
}Integer caching demonstration:
public static void main(String[] args) {
Integer a = new Integer(3);
Integer b = 3; // auto‑boxing uses cache for -128..127
int c = 3;
System.out.println(a == b); // false
System.out.println(a == c); // true (auto‑unboxing)
System.out.println(b == c); // true
Integer a1 = 128;
Integer b1 = 128;
System.out.println(a1 == b1); // false
Integer a2 = 127;
Integer b2 = 127;
System.out.println(a2 == b2); // true
}OOP vs Procedural
Procedural programming offers higher raw performance but lacks maintainability, reuse, and extensibility. Object‑oriented programming provides encapsulation, inheritance, and polymorphism, leading to more modular and adaptable code at the cost of some performance.
Four Pillars of OOP
Abstraction : Define essential characteristics without implementation details.
Encapsulation : Hide internal state behind public methods.
Inheritance : Reuse and extend existing classes.
Polymorphism : Allow a single interface to represent different underlying forms.
Exception vs Error
Both inherit from Throwable . Exception represents recoverable conditions (checked and unchecked), while Error indicates serious problems (e.g., OutOfMemoryError ) that applications should not attempt to handle.
Final, Finally, Finalize
final : can modify classes, methods, or variables to prevent inheritance, overriding, or reassignment.
finally : block that always executes after try / catch , typically for resource cleanup.
finalize() : method invoked by the garbage collector before object reclamation (rarely used).
Reference Types
Strong references prevent GC. Soft references are cleared only under memory pressure (useful for caches). Weak references (e.g., keys in ThreadLocalMap ) are cleared promptly when not strongly reachable. Phantom references work with a ReferenceQueue to track object reclamation.
String, StringBuilder, StringBuffer
String is immutable and thread‑safe. StringBuilder offers mutable, non‑synchronized operations for single‑threaded scenarios, delivering ~10‑15% performance gain over StringBuffer . StringBuffer provides synchronized mutable strings for multithreaded use.
HashMap with String Keys
Strings are immutable and cache their hashCode , making them fast and reliable keys for HashMap lookups.
Interface vs Abstract Class
Both cannot be instantiated and can contain abstract members. Interfaces define contracts (pure abstract methods, default/static methods in Java 8) and support multiple inheritance. Abstract classes can provide shared implementation and state but support only single inheritance. Prefer interfaces for behavior contracts; use abstract classes when shared code is needed.
Value vs Reference Passing
Java always uses pass‑by‑value. For primitives, the value itself is copied. For objects, the value of the reference is copied, so the method receives a reference to the same object, allowing mutation of the object's state but not reassignment of the caller's variable.
Conclusion
Mastering these core Java concepts—runtime architecture, language features, OOP principles, exception hierarchy, memory management, and common interview questions—forms a solid foundation for any Java developer and prepares them for technical interviews and real‑world development challenges.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.