Prathamesh Deshmukh
Prathamesh Deshmukh

Reputation: 78

String as immutable object

If String is a immutable object then is it possible to revert the changed values back? What i mean is that if there is one string variable

String str="Hello";

and if it is changed to

str="Hi!";

Now, as we know that it will create two string objects "Hello" and "Hi!" and at present str points "Hi!". So what i am asking is that is it possible to assign reference of string "Hello" (the previous one, not by creating new) to str as it is present somewhere in the memory?

Upvotes: 0

Views: 115

Answers (6)

Charaf JRA
Charaf JRA

Reputation: 8334

when you do str = "Hi"; , the first value of str is overriden and is not present in the RAM. if you want to preserve its value , you need to store it in temporary variable :

String str = "Hello";
String temp = str;//store Hello in temporary variable
str = "Hi!" ;
str = temp ; //first value is back

You can also , Use Arraylist<String> like a history or archive of the previous values.

Upvotes: 1

Martijn Courteaux
Martijn Courteaux

Reputation: 68847

Yes, you can do that. Java uses a String pool. So if you do:

String str = "abc";
str = "def";
str = "abc";

str will now point back to the original "abc" from the beginning.

However, if you use new Strings, you can force them to be the same by calling the intern method of String. This method will assure you that the returned String is in the shared String pool in memory.

String str1 = new String("abc").intern();
String str2 = new String("abc").intern();

Now, str1 == str2, which means that they point to the same String object.


To answer the question if you can undo an assignment: no. Your question was about Strings, but the same idea applies for everything.

int k = 4;
k = 9;
// now reverting to the "previous value" is impossible without hardcoding 4 again
k = 4;

You might think that it is different when you are using Strings, but it is not. The only difference is that a String object is somewhere in memory and that an int is only 4 bytes in memory.

Upvotes: 1

Guga
Guga

Reputation: 37

As the String "Hello" is still in the String Pool, and will be there for a while, you can reference any variable to it:

str = "Hello";
str = "Hi";
String s = "Hello"; //its the same Hello as the first one

The only case where you won't reference the same string is i you do

String s = New String ("Hello"); //this will be a different "Hello" from the first one.

Upvotes: 0

Jans
Jans

Reputation: 11250

The answer is yes, When a String is created is placed in handled memory string pool,In this case, JVM searches the string pool to see if equivalent string exist already. if yes, returns the reference to same. if not, adds it to string pool and returns the reference. So a new object may be created OR may not be.

Upvotes: 1

Sajan Chandran
Sajan Chandran

Reputation: 11487

You can do

str = "Hello"

JVM tries to look for the String Hello in its String pool if it finds it will reassign your variable str to Hello

Upvotes: 1

Marko Topolnik
Marko Topolnik

Reputation: 200138

  1. Your code creates zero objects. All string literals are created at class loading time, before any code execution happens.

  2. Wherever you use the same string literal in code, you refer to one and the same object. Therefore it is very easy to refer again to your "Hello" string at any point.

Upvotes: 0

Related Questions