• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Quick == question

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I don't understand why the result is "False". I think on both side of == , the references both point to the same object, which is 127. Can someone explain?
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The toString() method of the class Byte creates a new String every time you call it and returns that new String object. Therefore, when you call b1.toString() in each case, you'll get a different String object. Each object will, however, contain the same value, so b1.toString().equals(b1.toString()) would return true.
I hope that helps,
Corey
 
Wei Du
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Corey: thanks for the quick reply. I was thinking about the fact that JVM uses the "object pool" concept on String that it doesn't create a new String obect when it detects there is already one in the pool.
I guess toString() was implemented to always create a new String object instead of following the "String Pool" idea.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "string pool" is more correctly referred to as the "string literal pool." That pool is only used when you're dealing with string literals. As you have no string literals, you're not using the pool. Rather, new String objects are created with each invocation of toString.
Corey
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't forget this big difference:
String s1 = "Java";
String s2 = "Java";
System.out.println(s1 == s2); //prints true
String s3 = new String("Java");
String s4 = new String("Java");
System.out.println(s3 == s4); //prints false
 
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a difference between String literal and String object. When you say
String s = new String ("Hi");
There are two Strings. One is in the pool (because you used "Hi" as argument" and the other is an object s. in case of b1.toString() there are two String objects and hence == comparison fails
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need some help refreshing my memory what is the method to ensure no duplicates in the string literal pool.
Thanks.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ibrahim Hashimi:
I need some help refreshing my memory what is the method to ensure no duplicates in the string literal pool.
Thanks.


Pardon?
There are never any duplicates in the String literal pool. That's the purpose of the pool - to ensure that Strings that have already been created are reused.
Might you be referring to String.intern()? That method doesn't eliminate duplicates from the literal pool, it checks to see if a literal exists that matches the String object that you have and, if it does, the literal from the pool is used so that the extra String can be collected. Note, however, that the duplicate isn't in the pool - one is in the pool, one is not.
Of course, if no String literal exists that matches your String, a reference to your original String is returned.
Corey
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ibrahim ,
i think you mean intern()
String s = new String "hallo";
String s1 = new String "hallo";
if ( s.intern()== s1.intern()){
//prints true;
}
 
Sayed Ibrahim Hashimi
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Corey or Robert,
So based on that given the code below:

If I get this right I could say that there is 1 object that is eligble for garbage collection, is that right?
Thanks.
 
Robert Ziel
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ibrahim,
I think you are wrong..
after line 2 you have two references to String objects which are not referring to null
when you say s1 = s1.intern()
s1 references to the literal "test"
and according to my knowledge that's not null
Question what raises is when will s1 be eligble for gc ??
i think s1 will never be eligble for gc(during the scoop of the program) this because it refers now to the literal "test" which is stored on the heap
correct me when im wrong..

Robert
[ May 10, 2002: Message edited by: Robert Mmmo ]
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
s1 = new String("Java"); // puts an object on the heap
s2 = "Java"; // add a string to the literal pool
s1 = s1.intern(); //changes s1 to point to the literal pool entry
//and makes the entry in the heap eligible for garbage collection.
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Wei Du,
I did some in-depth research into a similar matter because this question is quite difficult to understand. Please check my findings at
https://coderanch.com/t/237614/java-programmer-SCJP/certification/String-problems. This may help you to understand how the toString() method works.
 
Robert Ziel
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thomas,
why is s1 eligle for gc
s1 contains the reference for the unique poolString so i would think not null
so not eligble for gc
could you tell me where i went wrong..??

Robert
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright, more pictures...

When you assign a reference to a String literal to a String variable, as I've done here, two things really happen. First of all, the String object is created and a reference to it is assigned to s1. However, when there is also a reference made to the object from a "String constant table." I've called that reference conRef, for constant reference. Note that this reference will never go out of scope. (Check out this thread for more details.)

In this case, I'm not using a String literal to create a new String (well, I kinda am, but not in the same way as before). In such a case, there is no extra "constant reference" created to the String object. Only the variable s2 references the object.

When you invoke the intern method on a String object, the JVM checks the String literal pool to see if there are any Strings there that are equal (have the same content) as the one you invoked the method on. In this case, there is only 1 String object in the literal pool, which is referenced by conRef. Indeed, that object is equal to the one we called intern on, so the reference conRef is returned, allowing s2 to "reuse" the String object that is in the literal pool. Now that the second String object has no references to it, it is now available for garbage collection.
I hope that helps,
Corey
P.S.
If you need help understanding these pictures, check out this thread where I explain them.
 
Wei Du
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all who participated in the discussion. Corey's reply cleared the confusion in my mind as toString() doesn't create a String literal, just a String object, thus String literal pool doesn't apply here.
 
Sayed Ibrahim Hashimi
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Robert Mmmo:
Ibrahim,
I think you are wrong..
after line 2 you have two references to String objects which are not referring to null
when you say s1 = s1.intern()
s1 references to the literal "test"
and according to my knowledge that's not null
Question what raises is when will s1 be eligble for gc ??
i think s1 will never be eligble for gc(during the scoop of the program) this because it refers now to the literal "test" which is stored on the heap
correct me when im wrong..

Robert
[ May 10, 2002: Message edited by: Robert Mmmo ]


Oops, I made a mistake when I typed in that code it should have been
s1 = new String("test");
for both, as Thomas pointed out so I guess they should be eligble for gc.
Thanks.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Robert Ziel:
Thomas,
why is s1 eligle for gc
s1 contains the reference for the unique poolString so i would think not null
so not eligble for gc
could you tell me where i went wrong..??

Robert, reread my comments paying careful attention to the difference between the heap and the literal pool. Remember, only objects on the heap are garbage collected.
 
Did Steve tell you that? Fuh - Steve. Just look at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic