💡 Quick Study Guide: Object References in Java
- 🔗 Reference Variables: In Java, a variable declared for an object (e.g., `String s;`) doesn't store the object itself. Instead, it stores a *reference* (a memory address) that points to where the actual object resides in the computer's memory (the heap).
- 📦 Object Instantiation: The `new` keyword is used to create an actual object in memory. For example, `String s = new String("Hello");` creates a `String` object "Hello" and stores its memory address in the variable `s`.
- 🔄 Assignment Behavior: When one object reference variable is assigned to another (e.g., `ref1 = ref2;`), it means both `ref1` and `ref2` will now point to the *exact same object* in memory. No new object is created; only the reference is copied.
- 🚫 Null References: A reference variable can hold the special value `null`, which signifies that it currently doesn't point to any object. Attempting to call a method or access a field on a `null` reference will result in a `NullPointerException` at runtime.
- 🗑️ Garbage Collection: An object becomes eligible for garbage collection (i.e., its memory can be reclaimed by the Java Virtual Machine) when no active reference variables are pointing to it anymore.
- ⚖️ Equality Operators:
== Operator: When used with object references, `==` compares the memory addresses (references) themselves. It returns `true` only if both references point to the *exact same object*..equals() Method: This method, defined in the `Object` class and often overridden in subclasses (like `String`), is used to compare the *contents* or *state* of two objects. For `String` objects, it checks if they contain the same sequence of characters.
- 🤝 Parameters (Pass-by-Value): When an object is passed as an argument to a method, Java uses "pass-by-value" for the *reference*. This means a *copy* of the reference is passed to the method. Both the original reference and the copied reference inside the method still point to the *same original object*. Therefore, changes made to the object *through* this reference inside the method will affect the original object outside the method.
🧠 Practice Quiz: Test Your Knowledge
- What does an object reference variable in Java primarily store?
A. The entire object's data
B. The memory address where the object is located
C. A copy of the object
D. The object's class name - Consider the following code snippet:
Student s1 = new Student("Alice");
Student s2 = s1;
After these lines execute, what is true about `s1` and `s2`?
A. `s1` and `s2` are two independent copies of the same `Student` object.
B. `s1` and `s2` both refer to the exact same `Student` object in memory.
C. `s2` now refers to a `null` object.
D. The `Student` object initially referenced by `s1` is now eligible for garbage collection. - Which of the following best describes the `null` value when assigned to an object reference?
A. It indicates that the variable stores an empty object.
B. It signifies that the variable points to a default, pre-defined object.
C. It means the variable does not point to any object in memory.
D. It creates a new object with no data. - An object becomes eligible for garbage collection when:
A. Its constructor finishes execution.
B. It is explicitly deleted using the `delete` keyword.
C. There are no active reference variables pointing to it.
D. It is no longer in scope within a method. - What is the primary difference between using `==` and the `.equals()` method when comparing two object references, `obj1` and `obj2`?
A. `==` compares the contents of the objects, while `.equals()` compares their memory addresses.
B. `==` can only be used for primitive types, while `.equals()` is for objects.
C. `==` compares if `obj1` and `obj2` refer to the exact same object, while `.equals()` (if overridden) compares their logical content.
D. Both `==` and `.equals()` always perform the same comparison, but `.equals()` is preferred for readability. - Consider a method `modifyObject(MyClass obj)` that takes an object reference as a parameter. If changes are made to `obj` inside this method (e.g., `obj.setValue(10);`), what happens to the original object passed from the calling code?
A. A separate copy of the object is modified, leaving the original unchanged.
B. The original object itself is modified because both references point to the same object.
C. The original object is replaced by a new object created inside the method.
D. The method creates a new object, and the original object becomes `null`. - What will be the output of the following Java code snippet?
String s1 = new String("Java");
String s2 = new String("Java");
String s3 = s1;
System.out.println(s1 == s2);
System.out.println(s1 == s3);
System.out.println(s1.equals(s2));
A. `true`
`true`
`true`
B. `false`
`true`
`true`
C. `false`
`false`
`true`
D. `true`
`false`
`false`
Click to see Answers
1. B
2. B
3. C
4. C
5. C
6. B
7. B