Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Basic concept for memory allocation

 
Robin John
Ranch Hand
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robin John wrote:please revive my concepts on this... dont have time to go to the books..

Strings Literally
 
Robin John
Ranch Hand
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robin,

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

John.
 
Robin John
Ranch Hand
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot! John..
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robin John wrote:does it mean the str and str2 if they return same hashcodes they are sharing the same memory location?

No.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic