This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes Basic concept for memory allocation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Basic concept for memory allocation" Watch "Basic concept for memory allocation" New topic
Author

Basic concept for memory allocation

Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

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
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Strings being pooled, yes, A.str and B.str hold the same reference. You could check it with System.out.println(A.str == B.str);


[My Blog]
All roads lead to JavaRanch
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

Robin John wrote:please revive my concepts on this... dont have time to go to the books..

Strings Literally

http://muhammadkhojaye.blogspot.com/
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

Still not clear.. and Muhammad the article talks about strings in same classes and new Strings..

I would like more inputs Thanks again.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Robin:

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).

John.
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

John de Michele wrote:Robin:

Why does it matter? Java handles all of this behind the scenes.
John.


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
Rancher

Joined: Mar 09, 2009
Posts: 600
Robin:

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.

John.
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

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
Rancher

Joined: Mar 09, 2009
Posts: 600
Robin,

Yes, you'll have one set of Strings, regardless of which class gets loaded first.

John.
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

Thanks a lot! John..
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

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.

Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

Robin John wrote:does it mean the str and str2 if they return same hashcodes they are sharing the same memory location?

No.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Basic concept for memory allocation