+ 1

Why doesn't the value of a object get changed when the value of its object acting as its reference is changed?

i created two objects of class String using "=" operator, and I initialised them with the same value. So JVM should allot them memory in the STRING CONSTANT POOL with same object reference i.e. one object will act as a reference variable for another object. Then, I changed the value of 1 object so value should reflect to the other object. But it didn't reflect. Why? https://code.sololearn.com/cqPk1RiMNl1k/?ref=app

30th Jun 2017, 8:03 PM
Divya Vishwakarma
Divya Vishwakarma - avatar
8 odpowiedzi
+ 4
*Strings are immutable.* If I wrote: String a "blah"; String c = "blah"; c = "jw"; The String with "jw" is a different String than the Strings with the value "blah". So once I said 'c = "jw"' the String c would not be at the same location in the SCP as the Strings with the value of "blah". In other words: The re-assignment created a new string.
30th Jun 2017, 9:33 PM
Rrestoring faith
Rrestoring faith - avatar
+ 3
You never actually created a reference. Using same value does not reference them.
30th Jun 2017, 8:14 PM
Jordan Chapman
Jordan Chapman - avatar
+ 3
these two variables don't have the same address, they only have the same values
1st Jul 2017, 6:32 PM
Jordan Chapman
Jordan Chapman - avatar
+ 3
@Jordan Two Strings with the same value will not have different memory locations. This is an optimization JVM makes for Strings. It's called the String Constant Pool or SCP. Proof: https://code.sololearn.com/cGquk1UvwLrJ/?ref=app
1st Jul 2017, 6:48 PM
Rrestoring faith
Rrestoring faith - avatar
+ 1
But don't u think Jordan that if two variables have same memory address, then one will act as an alias for the other one.
1st Jul 2017, 10:09 AM
Divya Vishwakarma
Divya Vishwakarma - avatar
+ 1
But as I read in a book, it was written that when two objects are created using with = operator, then they are stored in String constant pool.And if two objects have same value then the object which gets later created doesn't hold different memory address, it simply points to the same address of the previous object. So one object must act as a reference according to this scenario. But according to the principle of Immutability of strings, this doesn't happen so.
1st Jul 2017, 6:48 PM
Divya Vishwakarma
Divya Vishwakarma - avatar
+ 1
But as I didn't know the concept of Immutability earlier, so it confused me a bit that one should act as rma reference to other. But now the concept is clear. Thank you for helping me out friends. @Jordan and @restoring_faith 🤗
1st Jul 2017, 6:51 PM
Divya Vishwakarma
Divya Vishwakarma - avatar
0
There reference is just an address which the value sits in, changing the value does not change his address. There are many tools among the web for using references you can use. Just make sure you work slowly with them as it sometimes can get very complex, it can also lead you to errors within your code. There are tools detecting those kind of errors, such as checkmarx and others you can use. Good luck.
30th Jul 2017, 10:47 AM
Ben hart