• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Method parameter  RSS feed

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have some doubts in the following code snippet:
public class Why {
public static void main(String argv[]) {
String message = "hello";
new Why().run(message);
System.out.println(message);
}

public void run(String text) {
text += " world,";
System.out.println(text);
}
}
The output is "hello world, hello". I am wondering why the answer is not "hello world, hello world". As the method parameter being passed is a String object type, after the execution of the run() method, the parameter should be changed accordingly as well.
Thanks.
 
Ranch Hand
Posts: 1245
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Roger Chan:
I have some doubts in the following code snippet:
public class Why {
public static void main(String argv[]) {
String message = "hello";
new Why().run(message);
System.out.println(message);
}

public void run(String text) {
text += " world,";
System.out.println(text);
}
}
The output is "hello world, hello". I am wondering why the answer is not "hello world, hello world". As the method parameter being passed is a String object type, after the execution of the run() method, the parameter should be changed accordingly as well.
Thanks.


Java pass by value which means it makes a copy of (message) and pass that to the method (text). Since it is a copy it will go away when the method ends. So, the original reference (message) will be used again in your main method.
Thus,
message ----> "hello"
text (copy) ----> "hello" same hello.
text += " world,";
text ------> "hello world" new String Object.
when the method ended text goes away and "hello world" go away.

Hope this help.


[This message has been edited by FEI NG (edited November 20, 2001).]
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think method parameter is passed by value and not by reference
CMIW
 
Fei Ng
Ranch Hand
Posts: 1245
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry.. meant to write pass by value of reference.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is this because of the immutability of string?
for other data types like Vector, it's passed by reference, right? (just to make sure ^_^)
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by FEI NG:
Java pass by reference which means it makes a copy of (message) and pass that to the method (text). Since it is a copy it will go away when the method ends. So, the original reference (message) will be used again in your main method.

Thus,

message ----> "hello"

text (copy) ----> "hello" same hello.

text += " world,";

text ------> "hello world" new String Object.

when the method ended text goes away and "hello world" go away.
Hope this help.


What if
static String message="hello";
?
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Dong Fu:
What if
static String message="hello";
?


If variable message is modified by "static" , it means variable message assosiate with the Class, so only one message will exists. The variable message would be "hello world".
 
Fei Ng
Ranch Hand
Posts: 1245
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes... but if you are passing a static parameter the result will be the same.
 
Fei Ng
Ranch Hand
Posts: 1245
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Chin Loong:
is this because of the immutability of string?
for other data types like Vector, it's passed by reference, right? (just to make sure ^_^)


nah.. the reason on the above code wasn't becuase of the immutability of String object.
The code above just tried to change the reference on where it is pointing..
If what u said was right on the above code the output wouldn't be "Hello World, Hello"
it would be just "Hello, Hello"
But thats just the case for the above code. You still can't change the content of a String object.

[This message has been edited by FEI NG (edited November 19, 2001).]
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I still dont get it. Can somebody show me some books for reference.
 
Chin Loong
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fei ng : so, other than String, which other objects are passed by value like the question above, rather than passed by reference?
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chin loong: This illustration does not mean String objects are passed by value than by reference. Java does not have any such preference for any specific classes. What simply happens is with text += " world," statement a new String object is created and the reference is assigned to text which now looses the reference value passed to it while calling. When the called method run() finishes, the original reference value in the variable message which contains a reference to the String object "hello" is used again to print the original message.
If you replace String with StringBuffer and text += " world," with text.append(" world,") the output of the code will be "hello world, hello world,". Here the append method does not create a new StringBuffer object but allows you to modify the object being pointed by the reference stored in the variable text.
Manoj
 
Fei Ng
Ranch Hand
Posts: 1245
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Chin Loong:
fei ng : so, other than [b]String, which other objects are passed by value like the question above, rather than passed by reference?[/B]


All parameter passing is passed by value meaning you get a copy if what you are passing. If you are passing a int or long or float or double you are passing a copy of int or long or floast or double. If you passing an object reference you are passing a copy of that reference pointing the that same object.

check out this link.
http://www.javaranch.com/campfire/StoryPassBy.jsp

[This message has been edited by FEI NG (edited November 20, 2001).]
 
Ranch Hand
Posts: 5390
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
every param is pass by copy...
and at least in this example(or any other que. which involve String) be sure that nothing can change a string object as they are immutable(i.e. content cannot be changed).
for other questions..... understand carefully what happens when u pass an reference var. Fei has given link...
All The Best
------------------
Regards
Ravish
 
Chin Loong
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh! i understand now.. thanks, guys
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The address of the variable 'message' is passed to the run method. So, text now holds the addess of a location on the heap that holds the value 'hello'. Now the statement
text+="world";
creates a new string on the heap( note that it doesnot append world to text, as strings are immutable.) and the reference to the new string is passed to text. thus text now points to a memory location where 'Hello World' is stored.
while message variable still points to the same memory location where 'hello' is stored.
thus the result is 'hello world, hello'
hope that was clear.
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with ravish roger
even the sun course sl-275 says is pass by copy
all the best
------------

------------------
coffee drinker
[This message has been edited by amit mawkin (edited November 20, 2001).]
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps reading Pass-by-Value Please Campfire Story will help clear things up for you.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's an article by Peter Haggar,
Understanding that parameters are passed by value and not by reference that explains the difference as well.
Hope it helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
Co-author Mike Meyers' Java 2 Certification Passport
 
Roger Chan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for eveybody's input. I checked out the "Pass-by-Value Please" CampFire stories. This story did an excellent job of illustrating Java programming concept. I read other stories as well. They make learning Java fun. Great work for all the CampFire stories authors!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!