File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Difference between mutable & immutable striing? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between mutable & immutable striing?" Watch "Difference between mutable & immutable striing?" New topic

Difference between mutable & immutable striing?

sethuraman vanangamudi

Joined: Dec 16, 2004
Posts: 1
I am new in java.I attended one interview last week.the interviewer asked this question.
Ransika deSilva
Ranch Hand

Joined: Feb 18, 2003
Posts: 524
Hi Sethuraman,
According to my knowledge, in Java, all Strings are immutable, which means once you assign a value and then change it, what you get is a new string, not the same one. To over come this, there is a class called StringBuffer, where you can change the value. Hope this helps you. Good luck and Welcome to Java

SCJP 1.4, SCMAD 1.0<br />SCWCD, SCBCD (in progress)
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Some kinds of Java objects cannot be modified once they are created. That makes them immutable. If you think you are changing a String, you are really creating a new String.

However, there are StringBuffer objects that also hold a series of caracters and can be changed. That makes them mutable.
[ December 16, 2004: Message edited by: Mike Gershman ]

Mike Gershman
SCJP 1.4, SCWCD in process
I agree. Here's the link:
subject: Difference between mutable & immutable striing?
It's not a secret anymore!