• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Strign.replace() replaceAll()

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

Simple question, but it's something was thinking about today.

Why is it you can't do something like

String myString = new String("This is a string");
myString.replaceAll("s","X");

- ThiX iX a Xtring

Why do you have to do

myString = myString.replaceAll("s","X");

Just trying to remember, cheers in advance.
 
Greenhorn
Posts: 17
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When writing


you get a returned String which has all "s" replaced with "X".
But to be able to store the information you have to store it in a String which is what you do when you write


 
Marshal
Posts: 76863
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are reminded the String class is immutable. That is why you have to use the assignment.
 
Kevin P Smith
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheers guys!

immutable - That is/was the word I was looking for, the object can not be updated once it has been set.

But then I guess that would beg the obvious question... Why not?
Why have the Java guys not shorthanded the String object so that myString.replaceAll("s","X") updates the myString object's values 's' into 'X'. I guess there is a reason they haven't done this but just wonder why. I'd image that the majority of the time, when you're replaceing string values in a String object, you'd want the same object to contain that new value and if you did want the new value to be held in a new object then you could just do something like

String myString = new String("this is a string");
String anotherString = myString.replaceAll("x","X");

to make anotherString 'ThiX iX a Xtring'

As I say, I guess there's a reason they've done it this way.

Cheers again guys, thanks for the quick responses.
 
Frederick Winsnes
Greenhorn
Posts: 17
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quoted from: http://www.javapractices.com/topic/TopicAction.do?Id=29


Immutable objects are simply objects whose state (the object's data) cannot change after construction. Examples of immutable objects from the JDK include String and Integer.

Immutable objects greatly simplify your program, since they :

* are simple to construct, test, and use
* are automatically thread-safe and have no synchronization issues
* do not need a copy constructor
* do not need an implementation of clone
* allow hashCode to use lazy initialization, and to cache its return value
* do not need to be copied defensively when used as a field
* make good Map keys and Set elements (these objects must not change state while in the collection)
* have their class invariant established once upon construction, and it never needs to be checked again
* always have "failure atomicity" (a term used by Joshua Bloch) : if an immutable object throws an exception, it's never left in an undesirable or indeterminate state



It basically makes the program easier and safer to use and to program. (Correct me if I'm wrong)
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you need a modifiable string-type class, check out StringBuilder. It's methods will modify the object. Its toString() method can then at any time get a String with the current value.
 
BWA HA HA HA HA HA HA! Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic