• 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

DAN's Qn doubt

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First go through the code -->

class MWC106 {
static void m1(String s) {
s = s.trim();
s = s.concat("D");
}
public static void main(String[] s) {
String s1 = "A", s2 = " B ", s3 = "C";
m1(s2);
System.out.print(s1 + s2 + s3);
}}
In this code snippet, the output should be ABDC according to me. But the real output is ABC.

My explaination goes like this. in m1() method we are passing a String reference. so in the defination of method, it will get the copy of reference BUT both pointing to the same String Object. so we are modifying those object values in the method, it means the value objects are modified, when we print it in the main method using the rerence s2, it should get modified through method call.
So it should be ABDC. But the answer I know , I am wrong somewhere. please clarify me this.

Thanx & Regards,
Nandish
 
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
s = s.concat("D")

will assign new address to the local variable s. This address is never passed to the "main" to the answer "ABC". Remember String objects are immutable.
 
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

so in the defination of method, it will get the copy of reference BUT both pointing to the same String Object. so we are modifying those object values in the method, it means the value objects are modified

This always used to throw me too. Remember, Strings are immutable... they cannot be changed. so when the m1 method calls:

it actually creates a new String object with the value "BD", and assigns the address to the local variable s. The variable s2 still points to the original String object, which still has the value "B".

<<Didn't mean to replicate your answer Purushothaman, I didn't see your reply before I posted mine. >>
[ October 11, 2004: Message edited by: Paul Bourdeaux ]
 
Nandish Sri
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guyz,
Thanx for ur reply.. Yeah Strings are IMMUTABLE. I came to know one more point of Strings' immutability while passing Strings as arguments to methods . I think if u use StringBuffer instead of String we will get the answer as "ABDC". rt? Anyway thanx for ur reply...

Regards,
Nandish
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think if u use StringBuffer instead of String we will get the answer as "ABDC". rt?

Yep, although you would have to modify the code a little bit to use the StringBuffer methods. The following should return "ADBC"
reply
    Bookmark Topic Watch Topic
  • New Topic