string immutable

Posted on 2012-09-21
Medium Priority
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

Sharon Seth earned 1000 total points
ID: 38421171
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

ksivananth earned 1000 total points
ID: 38421187
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 75

Expert Comment

by:käµfm³d 👽
ID: 38421492
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


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month16 days, 9 hours left to enroll

862 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