string immutable

Posted on 2012-09-21
Last Modified: 2012-09-21

I was reading about immutable object. Immutable objects are those which once created cannot be changed and string is immutable

See the sample code below
class MainClass
	public static void main(String args[])
		String ABC = "test";
		ABC = "2344";

Open in new window

Here I have initialized a string variable and change its value by assigning a new value. Now the value is changed after the string variable is created so how string are immutable?

Karan Gupta
Question by:KaranGupta
    LVL 12

    Accepted Solution

    ABC = "2344";

    This does not change the value of the string(test) already being referenced by ABC . What happens is , a new String object gets created for storing 2344 , and ABC will be modified to refer to the new location
    LVL 26

    Assisted Solution

    what you have shown is, you are simply changing the reference of the object ABC. in the first line and second line, ABC points to two different object in your example

    what they meant is the value of the object can not be change when it is called immutable, for e.g.,

    String str = "test" ;
    String str2 = str.concat( " immutable" ) ;
    System.out.println( str ) ;
    System.out.println( str2 ) ;

    here I am calling concat method on str but the content of str doesn't change but it creates a new instance instead. but if you use StringBuilder

    StringBuilder sb = new StringBuilder( "test" ) ;
    sb.append( " immutable" ) ;
    System.out.println( sb ) ;

    here the content of sb itself gets changed. hope it clears.
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    They are immutable because you cannot do this:

    ABC[0] = "Q";

    Open in new window

    That is, you cannot manipulate the individual characters of the string. In order to change the string, you must create an entirely new string which already has the change:

    ABC = "test";
    ABC = "Qest";

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Title # Comments Views Activity
    withoutTen challenge 14 68
    classnotfoundexception 3 28
    Connect to IP Camera using Java 4 59
    Eclipse Neon and jdk 1.8.0 11 37
    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now