0% found this document useful (0 votes)
2 views2 pages

13 Core Java

The document provides an overview of core Java concepts essential for JVM Trainees, including JVM, JRE, JDK, memory areas, garbage collection, collections framework, exception handling, and generics. It also addresses common interview questions related to string immutability, the equals/hashCode contract, checked vs unchecked exceptions, and performance differences between data structures. Understanding these concepts will help candidates stand out in interviews by demonstrating a deeper knowledge of Java internals beyond basic syntax.

Uploaded by

princa3006
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

13 Core Java

The document provides an overview of core Java concepts essential for JVM Trainees, including JVM, JRE, JDK, memory areas, garbage collection, collections framework, exception handling, and generics. It also addresses common interview questions related to string immutability, the equals/hashCode contract, checked vs unchecked exceptions, and performance differences between data structures. Understanding these concepts will help candidates stand out in interviews by demonstrating a deeper knowledge of Java internals beyond basic syntax.

Uploaded by

princa3006
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Core Java

Overview
Given your JVM Trainee applications, a sharp grasp of Java internals — memory model, collections, and
exception handling — will differentiate you from candidates with only surface-level syntax knowledge.

Key Concepts
• JVM, JRE, JDK: JVM executes bytecode and is platform-specific; JRE bundles the JVM with core libraries
needed to run Java programs; JDK includes JRE plus development tools (compiler, debugger) needed to
build Java programs.
• Memory Areas: Heap vs Stack: Objects live on the heap (shared, garbage-collected); method calls, local
variables, and references live on the stack (thread-specific, LIFO, automatically reclaimed on method
return).
• Garbage Collection: Automatic memory management that reclaims heap memory occupied by unreachable
objects; generational GC (Young/Old generation) exploits the observation that most objects die young.
• Collections Framework: List (ArrayList, LinkedList — ordered, allows duplicates), Set (HashSet, TreeSet —
unique elements), Map (HashMap, TreeMap — key-value pairs) — each with different performance
characteristics for access/insertion/ordering.
• HashMap Internals: Uses an array of buckets with hashCode() to locate a bucket and equals() to resolve
collisions (via linked list, converting to a red-black tree for large buckets in Java 8+); average O(1) get/put.
• Exception Handling: Checked exceptions must be declared/handled (e.g., IOException); unchecked
exceptions (RuntimeException subclasses) don't require explicit handling; try-with-resources auto-closes
resources implementing AutoCloseable.
• String Pool & Immutability: String literals are interned in a special pool for reuse; Strings are immutable, so
every 'modification' creates a new object — this enables safe sharing and consistent hashing.
• equals() and hashCode() Contract: Objects that are equal must have the same hashCode(); overriding one
without the other breaks correctness in hash-based collections like HashMap/HashSet.
• Generics: Enable type-safe, reusable code (e.g., List<String>) with compile-time type checking, using type
erasure at runtime for backward compatibility.
• Functional Interfaces & Lambdas: Java 8 introduced lambdas and functional interfaces (Runnable,
Comparator, Function<T,R>) enabling more concise, functional-style code, plus the Stream API for
declarative collection processing.
Common Interview Questions
Q1. Why is String immutable in Java?
Immutability enables safe sharing via the string pool (saving memory), guarantees thread safety without
synchronization, allows consistent caching of the hash code for fast use in hash-based collections, and
prevents security issues from mutable strings used as parameters (e.g., in file paths, class loading).

Q2. What happens if you override equals() but not hashCode()?


You violate the equals/hashCode contract — two 'equal' objects could report different hash codes, causing
them to land in different buckets in a HashMap/HashSet, which breaks lookups and can result in duplicate
entries that should have been treated as the same key.

Q3. Explain the difference between checked and unchecked exceptions with examples.
Checked exceptions (like IOException, SQLException) are checked at compile time and must be declared or
caught; unchecked exceptions (like NullPointerException, ArrayIndexOutOfBoundsException, extending
RuntimeException) aren't required to be declared and typically indicate programming bugs.

Q4. How does HashMap handle collisions internally?


Each bucket holds entries with the same hash bucket index as a linked list (or, since Java 8, a red-black tree if
a bucket grows beyond a threshold for better worst-case lookup); equals() is used to find the exact matching
key within a bucket.

Q5. What is the difference between ArrayList and LinkedList in terms of performance?
ArrayList offers O(1) indexed access but O(n) insertion/deletion in the middle (due to shifting elements);
LinkedList offers O(1) insertion/deletion given a node reference but O(n) indexed access since it must
traverse from the head/tail.

Q6. What is the difference between == and .equals() in Java?


== compares references (memory addresses) for objects, or actual values for primitives; .equals() compares
logical/content equality as defined by the class's override — for Strings and most wrapper classes, this
means == may return false while .equals() returns true for two objects with the same content.

Q7. Explain try-with-resources and why it's preferred over manual finally blocks.
try-with-resources automatically closes any resource implementing AutoCloseable at the end of the block,
even if an exception occurs, without needing an explicit finally block — reducing boilerplate and preventing
resource leaks from forgotten close() calls.

You might also like