Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

String objects are immutable they can be shared. I do no get what this means...please help.

 
Greenhorn
Posts: 12
Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example:

String str = "abc";

is equivalent to:

char data[] = {'a', 'b', 'c'};
String str = new String(data);
"
This is the description given in Java Doc by Sun.

I wish to know what this sharing refers to.
Thanks in advance.
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sharing String has to do with the String pool. If I write the following:



While I created two Strings, they both point to the same String in the String pool, because Strings are immutable and will never change.
 
Ranch Hand
Posts: 104
Netbeans IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I cannt draw diagram here but i try .. for further refrence you can see chapter 6 of K&B


String class(Immutable)

1.String x="abc";
x.concat("def");
System.out.println("x= "+x);
//output is x= abc
/*Because no new assingment was made,
the new string ojbect created with the concat()
method was abandoned insatantly*/

2.String x="abc";
x=x.concat("def");
System.out.println("x= "+x);
//output is x= abcdef
/*we got a nice ne string out of the deal, but the
downsideis that the old string has been lost in string pool,
thus wasting memory*/
 
appu sharma
Ranch Hand
Posts: 104
Netbeans IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
StringBuilder or StringBuffer (mutable)


now i think you got your answer???
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vinit sharma wrote:StringBuilder or StringBuffer (mutable)


now i think you got your answer???



hi, the second poster already answered your question.

Java has temporary memory to store short strings. here is example:

String a = "abc"; String b ="abc";
if (JVM temporary memory has "abc") then (a and b always refer to the same object)

 
reply
    Bookmark Topic Watch Topic
  • New Topic