String Reference Variable(Java Questions)
- What happens when you use a String reference variable to modify a String?
Answer:
- A new string is created ( or a matching String is found in the String pool), leaving the original String object untouched
- The reference used to modify the String (or rather make a new String by modifying a copy of the original) is then assigned the brand new String object .
- What does a eference variable hold?
Answer:
A reference variable holds bits that represent a way to get to a specific object in memory (on the heap).
- What happens when you pass a reference variable?
Answer:
When you pass a reference variable, you are passing a copy of the bits representing how to get to a specific object.
- What do both the caller and called methods have identical copies of?
Answer:
Both the caller and called method will have identical copies of the reference.
1) Caller, 2) Caller method
- What do the 1) caller and 2) called method both refer to on the heap?
Answer:
The 1) caller 2) called method will refer to the same exact object on the heap.
- It does not matter whether you are passing 1) primitive or 2) reference variables, you are always passing a copy of the bits in the variable.
Answer:
- If you are passing an object reference variable, you are passing a copy of the bits representing the reference to an object.
Answer:
- What is the method capable of when you "Pass-By-value"?
Answer:
The called method can not change the caller's variable. When you implement "Pass-By-value", the called method can not change the caller's variable.
- What is a method capable of when you "Pass By Reference: For object reference variables the called method can change the object the variable referred to.
Answer:
Pass By Reference: For object reference variables the called method can change the object the variable referred to .
- Can the called method reassign the caller's original reference variable and make it refer to a different object or null?
Answer:
For object references, the called method cannot reassign the caller's original reference variable and make it refer to a different object, or null.