This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Strings Immutable ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Strings Immutable ?" Watch "Strings Immutable ?" New topic
Author

Strings Immutable ?

Sid Robin
Ranch Hand

Joined: Nov 24, 2007
Posts: 53
can anyone explain in simple terms how strings are immutable ?
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12950
    
    3

Immutable means that once a String object is created, its content cannot be modified.

Have a look at the methods in class String (look at the API documentation). You'll see that there are no methods that change the content of the String. Note that all methods that do something with the String, such as toLowerCase() and toUpperCase(), return a new String object - they do not modify the original String object.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
ashni Prakash
Ranch Hand

Joined: Dec 05, 2006
Posts: 50
Immutable means the created stirng object can never be changed.Just look at below example,

String s = "abc";
String s = s2;
s2 = concat.("def");

In above case s is referencing still "abc"
Only s2 now referenced to "abcdef"

check the below code,

String s = "hello";
s.concat("world");
System.out.println(s);

Output is "hello"
The object created on second line is "hello world".It doesnt have any reference so its lost though its existing.
Sid Robin
Ranch Hand

Joined: Nov 24, 2007
Posts: 53
s2 = concat.("def");


I think the statement must be s.concat("def") ?


Thank you a lot..............!

Java Ranch Rocks
nico dotti
Ranch Hand

Joined: Oct 09, 2007
Posts: 124
I don't know if this qualifies for a simple explanation, but I found it helpful to actually see how the bytecode comes out.

So do something simple like:


Than compile it and run javap -c to decompile it to bytecode. If you keep the program REALLY simple, you'll be able to see what's really going on with Strings behind the scenes. You don't have to understand the opcode to a tee (the operation instructions in the bytecode) to be able to see when it's added a string literal to the string pool (Constant Literal Pool) because it will say ldc for load constant, or when it creates a whole new String, etc. I'm sure some may discourage this, but I found it quite helpful. Give it a try, it couldn't hurt!
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Strings Immutable ?
 
Similar Threads
Question from Jamie's Book
immutable Strings
Strings and StringBuffers
Strings are immutrable?
String replace???(Urgent)