• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

classis problem -- pass reference in method

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have some questions about this thread. String is immutable as I know, s will not change only if we assign a new value. In line 1, s still "ball", right? But in line 2, s copy its reference to str, and pass it ref to modify method, str is point to "base" also. In line 3, "baseball" will assign to the str reference, str and s reference are the same which point the obj, why output the s value is "base"? Is it the reason of the String is immutalbe?
If use StringBuffer instead of String, the result is output "baseball". Or just handle String object with primitive policy? I fill confuse at this situation with String object, anyone would give me some ideas? thx in advance.
[ November 14, 2002: Message edited by: Kelvin Michael ]
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,Kelvin,
The problem lays in the 3 statement:
str += "ball"; // 3
Here let write the statement in another way.
str=str+"ball";
The right side evaluate to a "baseball" object, which is a new object and has nothing to do with
the "ball" object the str reference used to refer to.
Then the str reference bind to the new object "baseball".And at this time, str and s are bind to different objects(s still bind to "ball").
Hope this will help.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to know that in Java when a method is called a copy of the reference is made and sent.
i.e.
at line 2 you have a reference str which is pointing to "base". Then you call the modify(..) method and pass s as the parameter. At this point a copy of s is made and passed to modify(..) method. So, now you have two referneces pointing to "base". In the modify(..) method you are trying to add "ball" to "base". Since String is immutable a new String object is created which contains the value "baseball" At this point you have two references s which has the value "base" and str which has the value "baseball". Therefore this behaviuor is due to two factors
1. References are passed by value.
2. String objects are immuatble.
If you had used a StringBuffer, no new object would have been created. But you have used String and sinceany modification to String object always creates a new String object you have this sort of behaviour. Hope this helps
 
Vinodh Lakshminarayan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops a small mistake in typing. At line 2 you have a reference s not str. Sorry about that
 
Kelvin Mak
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String is so difference..., anyway I understand it now. Thank you Vinodh, Cherry!
 
reply
    Bookmark Topic Watch Topic
  • New Topic