Jump to content
  • Advertisement
Sign in to follow this  
lride

simple java question

This topic is 1069 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

String i = "3";
String i2 = i;
i = "0";
System.out.println(i2);
 
 
I expected this to print out 0, but it prints out  3.
Why?

Share this post


Link to post
Share on other sites
Advertisement

From what I can see:

 

First you set i = "3"

Then you set i2 = i (thus "3")

Then you set i = "0"

 

So your values are: i = "0", i2 = "3"

 

*and i2 is what you print out

Share this post


Link to post
Share on other sites

i2 points to the same address as i does. so since i becomes "0", shouldon't i2 also be "0"?

In Java Strings are immutable. Basically there is nothing* that you could do to i that would affect the value of i2. They originally referred to the same object, the string "3." So it is a reference to an object, with "3" being the object. When you set i to "0" it just changes the reference, while the string object remained unchanged.

 

 

Note: I will be the first to admit that I do not program in Java. I am a C++ and C# programmer. I am just going off of what I believe I remember when I did learn Java. If my information is wrong I apologize. 

*I think you could use reflection to mutate a string (based off my quick research)? 

Share this post


Link to post
Share on other sites

In Java Strings are immutable.


They are, but that does not matter here. The assignment
i = "0"
is syntactic sugar for
i = new String("0");
which should make what happens much clearer.

Share this post


Link to post
Share on other sites

 

In Java Strings are immutable.


They are, but that does not matter here. The assignment
i = "0"
is syntactic sugar for
i = new String("0");
which should make what happens much clearer.

 


No it's definitely not. Java guarantees that ALL occurrences of the literal "0" in source code are in fact the same object, and assigning that literal to a variable does NOT create a new instance. On the other hand, calling new String("0") guarantees that you have a new String instance whose value happens to be equal to "0". The two situations are not at all the same.

Share this post


Link to post
Share on other sites


*I think you could use reflection to mutate a string (based off my quick research)? 

 

If your code has sufficient permissions then yes you *could* use reflection to access the underlying character array that stores the string's value and mutate it. You'd be violating a very fundamental assumption (that Strings are immutable) and probably break all kinds of things in the core library. 

Share this post


Link to post
Share on other sites

First: i references "3"

Second: i2 references "3"

Third: i references "0"

 

So by printing i2, you print "3".

 

When you do i2 = i you're copying the value of the reference 'i', not the object that its referencing. So when you do 'i = "0"' you're overwriting the reference value for a reference to another object, but i2 still keeps the reference to "3".

 

This is orthogonal to string's immutability. You could make the same example with StringBuilders (aka, mutable strings) and it would behave the same.

 

Preeety sure Oracle's Java tutorials explain this stuff, you should check them out.

Share this post


Link to post
Share on other sites
The = operator in most Java-style languages means "assign value to variable" (regardless of it being a string or not).

It never means "overwrite a value with a new value".


It is extremely important to understand the difference between variables and values. It should be the #1 first thing any programmer learns. Edited by Nypyren

Share this post


Link to post
Share on other sites
Edit: not worth a fight and I'm under enough stress without opening a can of worms and arguing exact semantics and explanation methods. Edited by BitMaster

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!