0% found this document useful (0 votes)
1 views

Java_Core_Senior_PreJava8_Interview_Problems

Java is strictly pass-by-value, meaning it passes the actual value for primitives and a copy of the reference for objects, leading to potential misconceptions about pass-by-reference. The document explains the differences between '==' and '.equals()', emphasizing that '==' checks reference equality while '.equals()' checks value equality, and highlights the importance of overriding both methods in custom classes. It also details the class loading process in the JVM, memory management for primitive types and objects, and guidelines for properly overriding equals() and hashCode() in custom classes.

Uploaded by

pbecic
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Java_Core_Senior_PreJava8_Interview_Problems

Java is strictly pass-by-value, meaning it passes the actual value for primitives and a copy of the reference for objects, leading to potential misconceptions about pass-by-reference. The document explains the differences between '==' and '.equals()', emphasizing that '==' checks reference equality while '.equals()' checks value equality, and highlights the importance of overriding both methods in custom classes. It also details the class loading process in the JVM, memory management for primitive types and objects, and guidelines for properly overriding equals() and hashCode() in custom classes.

Uploaded by

pbecic
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Core for Strong Senior Developers (Pre-Java 8, Excluding Collections,

Concurrency, JMM)

How does Java handle parameter passing — is it pass-by-value or pass-by-


reference?
 Java is strictly pass-by-value.
 For primitive types:
 The actual value is passed to the method.
 For objects:
 The reference (memory address) is passed by value.
 The method receives a copy of the reference, so it can modify the object’s state, but not
reassign the original reference.
 Common misconception:
 Some developers think Java is pass-by-reference because object changes inside a
method are reflected outside — this is due to reference sharing, not pass-by-reference
semantics.

What are the differences between '==' and '.equals()' in Java?


 '==' compares object references — it checks whether two references point to the same
object in memory.
 '.equals()' is a method used to compare object values (content equality).
 For built-in types:
 Primitives (int, char, etc.): '==' compares values.
 Objects (e.g., String, Integer): '==' checks reference equality; '.equals()' checks value
equality (if overridden).
 You must override '.equals()' in custom classes to define meaningful equality.
 Also override '.hashCode()' when overriding '.equals()' to maintain contract in hash-
based structures.

What happens during class loading and initialization in the JVM?


 The JVM loads classes in the following phases:
 Loading — the class is located and loaded into memory.
 Linking — involves verification, preparation (allocating static fields), and optionally
resolution (resolving symbolic references).
 Initialization — static blocks and static field initializers are executed.
 Class is initialized only once per ClassLoader.
 Loading is typically triggered when:
 A class is first accessed.
 A static method or field is used.
 Explicitly via Class.forName().
How does Java manage memory with respect to primitive types and objects?
 Primitive types are stored directly in the stack or as fields within objects.
 Objects are allocated on the heap and accessed via references.
 Stack:
 Stores method frames, local variables (including primitive values and references to
objects).
 Fast access and automatically cleaned up on method exit.
 Heap:
 Stores all objects and class instances.
 Managed by the garbage collector — reclaims memory of unreachable objects.
 Key point:
 Only references are passed around — the actual object always lives on the heap.

How would you override equals() and hashCode() properly in a custom class?
 The equals() method must check logical equivalence, not reference equality.
 Steps to override equals():
 Check if 'this == obj'.
 Check if obj is not null and is of the same class.
 Cast and compare relevant fields.
 The hashCode() method must return equal hash codes for equal objects.
 Guidelines:
 Use a consistent and well-distributed hash function.
 Include the same fields used in equals().
 Example:
 Use prime numbers in hash computation: int result = 31 * result + field.hashCode();
 Violating the equals-hashCode contract breaks behavior in hash-based collections.

You might also like