• 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

why not output is helloworld

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class XIx
{
public static void main (String[]args)
{
String s = new String ("Hello");
modify(s);
System.out.println(s);
}
public static void modify (String s)
{
s += "world!";
}
}

hi all,
the output for the above program is "Hello" why not the output is "Hellocome"

thanks,
venkat
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
pls, take care the scope of the String. If you modify your code,
you will get the desire output.

public class XIx
{
public static void main (String[]args)
{
String s = new String ("Hello");
String str=modify(s);
System.out.println(str);
}
public static String modify (String s)
{
s += "world!";
return s;
}
}
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are a couple things going on here. remember that s is a REFERENCE to the string object. so when you call modify(s), you are passing a copy of the reference into the method.

you effectivly now have TWO s variables pointing to the object - one from your main method, and one in the modify method.

insidy the modify method, you say "s += "world!";

what this does is says "Make a brand new string, and create it from what this local s variable points to plus the literal "world!". then, reassign that local variable to point to the new string." At this point, your local s points to a brand new string, while the s out in your main method still points to the original string.

your modify() method then exits. the local s goes out of scope, and is lost. the String object with the value of "Helloworld!" is now eligible for garbage collection, since nothing points to it.

you then print what the s from main refers to, which is the "Hello" string. This string, and this reference never changed.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In short

String's are immutable

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic