This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
The output is correct. Here's what's happening "apple juice" (from the System.out in the transform() method) "apple orange" (the fruit1 in the growFruit() is "apple" and the fruit2 is "orange" since transform() returns "orange")
Hope it's clear enough for you.
SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
Joined: Sep 07, 2008
Actually the transform() method has two output, 1.System.out.println(fruit1)--->"apple juice". 2.while returning---->"orange".
So after invoking growFruit(), fruit1 has the value "apple" and fruit2 has the the value,"apple juice","orange". still I am not clear.
I have modified the code a little bit. First fruit1 is "apple", then when transform is called, fruits is assigned "apple". When fruits = fruits + " juice"; is executed, after that fruits has the value "apple juice" (fruit1 in main is still "apple"). Now fruits is displayed so "apple juice" is displayed. Then the method returns "orange". This value get's assigned to fruit2. Now fruit1 is displayed which displays "apple" and fruit2 is displayed which displays "orange". So the final output is
String fruit2 = transform(fruit1); ----->while invoking this method fruit2 will contain "apple juice" and "orange".am i right?
I think you are confused here. When transform is called, fruit1's value will be copied to fruits (as per my code). Then the method will return "orange". So fruit2 will contain "orange". "apple juice" has no concern with main method. It will be displayed in transform method...