Hi guys, Please provide me with some inputs for this basic concept...
lets say I have a class
My Question is when class C is run, is it accessing A.str and B.str2 from different memory locations? or putting it in a different question - do str and str2 occupy two memory locations (the value being the same) ? or is it the reference point to same location which has the value "0" in it ? please revive my concepts on this... dont have time to go to the books.. stuck in some place nevermind..
Thanks in advance.
Time is what we want the most, but what we use the worst. -- William Penn
Why does it matter? Java handles all of this behind the scenes. Generally, how Java manages memory should be the last thing on your mind. Christope told you exactly how to test for different String objects: use the == operator, which for objects tells you whether references are the same (not if the Strings are equivalent).
Why does it matter? Java handles all of this behind the scenes.
so I should not be bothered if I create lets say a million Strings with different values and another million duplicating those previous values - will java take care of that? - if yes how?? by not assigning new memory to duplicate final strings or by garbage collecting?
and does it mean the str and str2 if they return same hashcodes they are sharing the same memory location?
John de Michele
Joined: Mar 09, 2009
Java has the concept of a String pool. So, if you do something like this:
Java will add this String to the String pool. If, later on, you do this:
The references string and string2 will both refer to the same String object. If you then do this:
Then you will be adding a new String to the String pool, even though it is equivalent to the original String object. So, if you add your million Strings as literals, and then add another million references to them, you'll have one million String objects, each with two references. If, instead, you add a million Strings, and then create a million more equivalent Strings, you'll have two million strings in the pool, each with one reference. Generally, you should use String literals when creating Strings, rather than creating new objects.
If you want to know if the Strings are referring to the same object, use the == operator. If you want to know if the Strings have the same values, use the equals() method. Whether any two Strings match either or both of those depends on how they were created, although you can be certain that if the values of two Strings do not match, then they can't be referring to the same object.
okay fantastic! it's all clear.... Please correct me if I am wrong for the below "specific case" ( considering static final constants in different packages and class) - "In the below code ..
when A and B get loaded, depending which ever first gets loaded (lets assume A gets loaded)... so when B gets loaded all the strings are just references to the same memory locations created by A's Strings right?
John de Michele
Joined: Mar 09, 2009
Yes, you'll have one set of Strings, regardless of which class gets loaded first.
Robin John wrote:
so I should not be bothered if I create lets say a million Strings with different values and another million duplicating those previous values - will java take care of that? - if yes how??
See for example when you write
Well then there's a literal "Muhammad" in the string pool. The compiler will replace all instances of "Muhammad" with this single reference to the value in the pool. These values are never garbage collected unless the classes that defined them are unloaded.
Also note that instances of string created at runtime are eligible to garbage collection just like every other object.