• 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
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

replace()

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
we all are know that String is immutable and StringBuffer is not(mutable).But why String has replace() method?It causes String can be modified by some means.However replace is there for StringBuffer-it is OK!What is actual meaning of immutable?
So i need some explanation about it.
Can anyone explain?please.

Thanks in advance.
---shiva
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you look at the syntax
String replace(char oldChar, char newChar)
String s = "abc";
s.replace('c','d');
The above code creates a new String "abd" maintaining same value in s.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi dilip,
Can u please tell me which is essential in String context
Number of characters inside string not to changed or
String type means (as u mentioned,) from "abc" to "abd"
--shiva
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not getting your doubt..
Let me explain something.
String s = "java";
s.toLowerCase --- No new object created
s.toUpperCase --- new object created
s.trim --- No new object created
s.replace('j','v') --- new object created
String x = "JAVA";
x.toLowerCase --- creates new object
x.toUpperCase --- No new object created
x.trim --- No new object created

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi dilip

my doubt is why String has replace()? because string is immutable object i.e can not be modified. but replace () completely modifies string object.is it OK!
--SHIVA
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shi,
There is no problem with String having a replace() method. Infact, String can have any kind of method which modify the string (if so required). The only point to note in all these methods is that they produce new objects as their output (there are exceptions - refer the API for that). For StringBuffer, the update methods do not produce a new object. That is the only difference.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
dilip and skrish very very thanks for your help
now my doubt is cleared.
--shiva
 
There is no "i" in denial. Tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic