Java Flashcards

1
Q

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

A

== Operator:
The == operator is used to compare primitives and object references.
For primitives (like int, char, float, etc.), it compares the values. For example, 5 == 5 is true.

.equals() Method:
The .equals() method is used to compare the contents of two objects.
It’s defined in the Object class, and it can be overridden in custom classes to define what it means for two instances of that class to be considered “equal”.

The default implementation of .equals() in the Object class behaves like ==, comparing memory addresses. However, many classes in Java’s standard libraries, like String, Integer, and other wrapper classes, override .equals() to compare the actual data within the objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain the concept of Java’s memory model, especially the stack and the heap

A

Java’s memory model primarily involves the stack and the heap, which are used for different types of memory allocation and management during the execution of a Java application.

The Stack
Function: The stack is used for static memory allocation and execution of threads. It stores primitive values and references to objects that are being used in a method.
Structure: It operates on a Last-In-First-Out (LIFO) principle. Each thread in a Java application has its own stack.
Content: The stack contains “stack frames,” each representing a method call. A stack frame holds local primitive variables, references to objects in the heap, and the method’s return value.
Lifecycle: When a method is invoked, a new frame is pushed onto the stack, and when the method completes, the frame is popped off.
Memory Management: Memory allocation and deallocation in the stack are automatic and fast.
Limitation: Stack memory is limited in size, and extensive use, such as in deep recursion, can lead to a StackOverflowError.

The Heap
Function: The heap is used for dynamic memory allocation for Java objects and JRE classes at runtime.
Structure: It is a larger memory pool shared by all threads. The heap is further divided into different regions like Young Generation, Old Generation, and PermGen (or Metaspace in later versions of Java).
Content: It stores all Java objects created in your application, regardless of their scope. The objects are stored in the heap until they are no longer referenced and eligible for garbage collection.
Lifecycle: Objects in the heap have longer lifetimes and stay in memory while there are references to them. When there are no more references, they are marked for garbage collection.
Memory Management: The heap is managed by the Garbage Collector, which removes objects that are no longer in use to free up memory. The process is automatic but can be triggered manually with System.gc().
Limitation: Being larger than stack memory, the heap is less constrained, but inefficient use (like creating unnecessary objects) can lead to a OutOfMemoryError if the heap gets full.

Interaction Between Stack and Heap
When a new object is created, it’s stored in the heap, and a reference to it is placed on the stack (if it’s within a method).
Methods use the stack to keep track of their local variables and the execution flow, while actual objects are stored in the heap.
Stack and heap are complementary: stack memory is used for quick, short-lived data like method frames, while heap memory is used for data that needs to survive beyond method calls, like objects and their fields.
Understanding the stack and heap is essential for optimizing memory usage, avoiding memory leaks, and ensuring the performance of a Java application. It’s also crucial for debugging issues related to memory management.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly