• 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

Strings

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can Somone give me some example on Strings that can't be modified & String Bufffer that can be modified ?
Thanks
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
consider the following example :
String str1 = new String ("chunky");
String str2 = str1 ;
str1 = str1 + " shah " // a new string object str1 is created here
if (str1 == str2 ) System.out.println("refers the same object ");//NO
if (str1.equals(str2) System.out.println("has the same contents) ;//NO
//i.e once a string object is modified an altogether new String object refers to it (String objects are immutable)
//here s an example of string buffer
StringBuffer str3 = new StringBuffer("chunky");
StringBuffer str4 = str3; //both now point to the same object
str3.insert(6 ,"shah"); //str3 still refers to the same object even on modification
if (str3 == str4 ) System.out.println("refers the same object ");//YES
//i.e StringBuffer objects on modification too refer to the same old object
hope this is clear enuf
regards
arawin

 
permaculture is a more symbiotic relationship with nature so I can be even lazier. Read tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic