File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings are immunable - What does this mean?

 
Thomas Markl
Ranch Hand
Posts: 192
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Test58 {
static int i;
public static void main(String args[]) {
System.out.println("i "+i);
String s1= "Hello";
System.out.println(s1.replace( 'e' , 'e' ) == " Hello ");//String immutability
System.out.println(" Hello " . replace ( 'e' , 'e' ) == " Hello "); //Pooling of String
}
}
C:\Java\EigeneJavaProgramme>java Test58
i 0
false
true
Why does it print out "false" and "true".
Why is " Hello " . replace ( 'e' , 'e' ) == " Hello "
true and the other replace with s1 false?
Thomas
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is String s1= "Hello"; correct? Are you sure it's not String s1= " Hello " with two spaces at the front and back?
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the replace method does not change a String it returns the same String that the method was passed.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, the word is "immutable".
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Main Entry: im�mu�ta�ble
Function: adjective
Etymology: Middle English, from Latin immutabilis, from in- + mutabilis mutable
Date: 15th century

http://www.m-w.com/cgi-bin/dictionary

Synonyms: INFLEXIBLE, constant, fixed, immovable, inalterable, invariable, unalterable, unchangeable, unmodifiable, unmovable
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason I asked my question about the string "Hello" back up there is because you get a true comparison instead of a false one when you do the s1.replace( 'e' , 'e' ) == " Hello ".
-Barry
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic