• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

please clear me this...

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Sheriff
Posts: 9701
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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"
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9701
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Because of Shadowing.........
[ November 08, 2008: Message edited by: Srinivas Rachamallla ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic