• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Pass by value or Reference

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
void method1(){
String fruit = "apple";
transform(fruit);
System.out.println(fruit);//<----line 4
}

void transform( String fruit){
fruit = "orange"
}


from the code above why is it in line 4 fruit is still equal to "apple" but not "orange", isn't passing object to a method pass by reference? since String is an Object..directly extends Object.
[ November 06, 2008: Message edited by: joseph gonzales ]
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First thing fruit is method variable and local to method1. So it cannot be modify by other method i.e. transform (String fruit) without returning value to that method. Main region of not getting modify is the void return type of transform method.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The life of the fruit = "orange" ends after returning from the transform method i.e, it(fruit) is local to method.

Where as the fruit = "apple" is local to the method1() and as it is not a static variable the changes are unaffected
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi joseph,

Strings are immutable, in your code there are two strings in constant string pool now, "apple" and "orange". The method transform(fruit) will indeed pass a copy of the reference that refers to the same object, but the body of the transform() method, specifically the fruit = "orange" will Create a New String Object (not modify) on the String pool (remember Immutable) and now refers to a New String object "Orange".

Btw, are you from PH (kabayan)
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
void method1(){
String fruit = "apple"; //1
transform(fruit); //2
System.out.println(fruit);//6
}

void transform( String fruit){ //3
fruit = "orange" //4
}//5

Within transform(), there are two items to consider: a -reference- and an "object that reference points to." The local reference, named -fruit- exists only within transform(), while "apple", pointed to by -fruit- still exists within scope of method1().

In line 8, the reference, -fruit- is reassigned to a new string literal, "orange." The original object, "apple" remains untouched. Additionally, Strings have no way of being altered, as they are "immutable" (unchangeable).

A walkthrough (see line numbers above):
1. The String literal "apple" is assigned to reference -fruit-
2. transform() is called with a COPY of the reference, -fruit-
3. the COPY of the reference to "apple" is created, also named -fruit-
4. A new string literal, "orange" is assigned to the new local reference -fruit-
5. transform() ends, and the local copy of the -fruit- reference is gone
6. The original reference, -fruit- remains untouched, as only a copy of the reference was manipulated within transform(). "apple" is printed.

If these were StringBuffer objects, and instead of variable reassignment we saw fruit.append("orange") - the object would be changed to "appleorange". But because this was only a local variable reassignment, the object remained untouched.

I hope that helps. Let me know - good luck!
 
joseph gonzales
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey thanks to all!!

@Micheal
Yup taga Cebu ako..thanks

@Stevens
the walkthrough helps a lot..


Now I get it. Here my Understanding , I hope I get it right.

void method1(){
String fruit = "apple"; //1
transform(fruit); //2
System.out.println(fruit);//6
}

void transform( String fruit){ //3
fruit = "orange" //4
}//5

excutions steps
1. reference to an object created named fruit and assigned a value "apple"
2. a copy of reference is passed
3. a local reference also named fruit is now pointing to the same value "apple"
4. Now change the value it pointing to into "orange" (it is now pointing to the address in memory that contains the value "orange")
5. the local reference fruit is discarded. gc?
6. the orignal reference fruit is still pointing the same address in memory that holds the value "apple"

so in this case there is no way an original reference will be modified without returning the modified copycat reference value and assigned it into original reference..
 
Java Cowboy
Posts: 16084
88
Android Scala 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 joseph gonzales:
... isn't passing object to a method pass by reference? since String is an Object..directly extends Object.


No. Java is always pass by value. Note that variables in Java do not directly represent objects; they are references to objects. So you cannot technically pass an object to a method - you are really passing a reference to an object to a method.

Note that, since everything is passed by value, references are also passed by value. Passing a reference by value is not the same as passing by reference.

Have a look at: Pass by Value, Please
 
I didn't say it. I'm just telling you what this tiny ad said.
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic