• 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

string methods...

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s="ganesh";

it create one object in pool

s=s+"kumar"; ----it can craete new object..

similarly

if we can use toString(),toUpperCase() and substring() methods

Are these methods create new String object or not?

can anyone explain it?
[ December 18, 2008: Message edited by: Ganeshkumar cheekati ]
 
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Most of the functions that operate on objects of the "immutable" String class are optimized; they do not create duplicate objects when nothing changed in the original string object on which these methods were invoked.

This fact can be ascertained by reading the (javadoc)API documentation and studying the String.java class..
[ December 18, 2008: Message edited by: Monu Tripathi ]
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For Strings we have something called a String Pool .If after these operations ,the resulting string is already present in the pool a new instance is not create it just points to the exsisting one .If it isnt there then a new string is added .
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as i think, strings create new objects but we may not be able to refer those oblects.

like String s="abc"
s.toUppercase() will create new string "ABC" but its referance is lost
same is with s.subString()
But s.toString() method will return the referance to the current string only thats why it does not create new object.

Correct me if i am wrong

Thanks,
Geeta Vemula
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
all time that you run you are creating a new object. always!
 
reply
    Bookmark Topic Watch Topic
  • New Topic