File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes please clear me this... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "please clear me this..." Watch "please clear me this..." New topic
Author

please clear me this...

Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
Hi,
I got it from www.irixtech.com

class Fruit {
public static void main(String[ ] args) {
Fruit fruitTree = new Fruit( );
fruitTree.growFruit( );
}

void growFruit( ) {
String fruit1 = "apple";
String fruit2 = transform(fruit1);
System.out.print( fruit1+ " " +fruit2);
}

String transform(String fruit1) {
fruit1 = fruit1 + " juice";
System.out.print(fruit1 + " ");
return "orange";
}
}

I thought the answer would be "apple apple juice orange".But...


output: apple juice apple orange

please explain what is going on here?


Thanks in advance

Preparing Scjp5
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

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
My Blog
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
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.

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17



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

"apple juice apple orange"


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
dee anderson
Greenhorn

Joined: Oct 10, 2008
Posts: 16
remember that fruit1 is passed to transform function by value .. what happens to fruit1 inside the transform function is discarded when returning to the growFruit function
joseph gonzales
Greenhorn

Joined: Jun 21, 2008
Posts: 6
Hi

Why is it the fruit1 in growFruit() after invoking the transform() is still equal to "apple" but not "apple juice" isn't it pass by reference? since String is an object.
thanks..




preparing for SCJP
[ November 06, 2008: Message edited by: joseph gonzales ]
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
Hi Ankit,
class Fruit
{
public static void main(String[ ] args)
{
Fruit fruitTree = new Fruit( );
fruitTree.growFruit( );
}
void growFruit( )
{
String fruit1 = "apple";
String fruit2 = transform(fruit1); ----->while invoking this method fruit2 will contain "apple juice" and "orange".am i right?
System.out.print( fruit1+ " " +fruit2); ----->here, fruit1 has "apple".
}
String transform(String fruits)
{
fruits = fruits + " juice";
System.out.print(fruits + " ");
return "orange";
}


how come the "apple juice" comes first?
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
Hi Ankit
I got it clear now,
System.out.println has to do its work first.
Am i right?

correct me if i am wrong.

Thanks for the explanation.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Originally posted by Preetha Arun:

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...
Srinivas Rachamallla
Greenhorn

Joined: Feb 22, 2008
Posts: 10



Because of Shadowing.........
[ November 08, 2008: Message edited by: Srinivas Rachamallla ]

Srinivas.<br />Life is all about Comparisons.
 
 
subject: please clear me this...
 
Similar Threads
using `instanceof`with an "object" parameter
Constructor of parent abstrat class not getting executed.
Comparator giving compiler error
class design problem
Pass by value or Reference