This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
} } please solve my problem it is very basic but ... i have a dought the above given code should print 7.0 as object is passed by referance and we r assigning d to new refernce they why it showing me out put 5.0 instade of 7.0 can i get the out put like 7.0 please explain me how? thanks Kavan Preaparing for Scjp 1.4.0
In a nutshell, the main method instantiates object d, and will continue to hold a reference to it, no matter what is done in overWrite(). Even though overWrite() abandons it's reference to the Double object with a value of 5.0 and creates a new one with a value of 7.0, main() still holds the original reference with a value of 5.0.
This type of manipulation only works with mutable (changeable) objects. Since Double is immutable, the only way you can change it is to drop the reference and create a new one.
If the object passed as a parameter were a mutable object, such as a StringBuffer, overWrite could manipulate the object, and main() could see the results of that manipulation. However, it is impossible for a called method to make the calling method give up or replace it's reference to an object.
We're glad to have you stick around. Please change your name to match our standards, though, as that helps us to maintain a friendly atmosphere. Also, choose the forum carefully to increase your chances of getting a good and quick answer. The SCJP or the Java in General (Beginner) forums would have been perfect.