Blog/Java/Core Java

Core Java

10 essential core java interview questions

Java is a general-purpose, object-oriented programming language. Key features: platform-independent (write once, run anywhere), strongly typed, garbage collection, extensive libraries. Used for: enterprise apps, Android development, microservices, backend systems. JVM (Java Virtual Machine) executes compiled bytecode.
JVM (Java Virtual Machine): runtime environment executing bytecode. JRE (Java Runtime Environment): JVM + libraries for running Java apps. JDK (Java Development Kit): JRE + tools (compiler, debugger) for development. You need JDK for development, JRE to run apps.
Boolean (true/false), byte (8-bit integer), short (16-bit), int (32-bit), long (64-bit), float (32-bit floating point), double (64-bit). Wrapper classes: Boolean, Byte, Short, Integer, Long, Float, Double. Auto-boxing converts primitives to wrappers. Use primitives for performance, wrappers for collections/nullability.
String: immutable, thread-safe, creates new object on change. StringBuilder: mutable, not thread-safe, efficient for building strings. StringBuffer: mutable, thread-safe (synchronized), slower than StringBuilder. Use String for constants, StringBuilder in loops, StringBuffer in multi-threaded code.
Heap: objects stored here, garbage collected. Stack: method calls and local variables. Garbage collection: automatic freeing of unreachable objects. GC types: Serial (stop-the-world), Parallel (multiple threads), Concurrent (minimal pause). Monitor GC with profilers. Memory leaks: objects referenced but no longer needed.
Arrays: fixed size, faster access, store primitives/objects. Collections: variable size, slower but flexible. ArrayList (resizable array), LinkedList (linked nodes), HashSet (unique unordered), HashMap (key-value pairs), TreeSet/TreeMap (sorted). Collections framework provides powerful abstractions.
Try-catch block catches exceptions. Try: code that might throw. Catch: handles specific exception. Finally: always executes (cleanup). Throws declaration: declares method throws exception. Exception hierarchy: Throwable > Exception > (Checked/Unchecked). Checked: must catch. Unchecked: RuntimeException, optional to catch.
Abstract class: partial implementation, can have state (fields), constructors. Interface: contract only (Java 8+ default methods), no state, static methods. A class extends one abstract class, implements multiple interfaces. Use abstract class for related classes, interface for unrelated contracts.
Polymorphism: objects behave differently based on type. Compile-time (method overloading): same method name, different parameters. Runtime (method overriding): subclass overrides parent method. Allows same code to work with different types. Enables flexible and extensible design.
Multithreading: multiple threads running concurrently. Extend Thread or implement Runnable. Synchronize shared resources to prevent race conditions. Methods: synchronized keyword, volatile, AtomicInteger, Lock. Thread states: new, runnable, running, blocked, terminated. Useful for responsive UIs, parallel processing.