aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Quick == question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Quick == question" Watch "Quick == question" New topic
Author

Quick == question

Wei Du
Greenhorn

Joined: May 03, 2002
Posts: 16

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?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
Wei Du
Greenhorn

Joined: May 03, 2002
Posts: 16
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

Joined: Dec 20, 2001
Posts: 3271
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
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Chintan Rajyaguru
Ranch Hand

Joined: Aug 19, 2001
Posts: 341
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


ChintanRajyaguru.com
SOADevelopment.com - Coming soon!
Sayed Ibrahim Hashimi
Ranch Hand

Joined: May 17, 2001
Posts: 148
I need some help refreshing my memory what is the method to ensure no duplicates in the string literal pool.
Thanks.


SCJP 1.4<br /><a href="http://www.cise.ufl.edu/~sih" target="_blank" rel="nofollow">www.cise.ufl.edu/~sih</a>
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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
Robert Ziel
Greenhorn

Joined: Apr 22, 2002
Posts: 28
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

Joined: May 17, 2001
Posts: 148
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

Joined: Apr 22, 2002
Posts: 28
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
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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.
Mag Hoehme
Ranch Hand

Joined: Apr 07, 2002
Posts: 194
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
http://www.coderanch.com/t/237614/java-programmer-SCJP/certification/String-problems. This may help you to understand how the toString() method works.


Mag
Robert Ziel
Greenhorn

Joined: Apr 22, 2002
Posts: 28
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

Joined: Dec 20, 2001
Posts: 3271
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

Joined: May 03, 2002
Posts: 16
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

Joined: May 17, 2001
Posts: 148
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
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Quick == question
 
Similar Threads
toString()
Byte and String
* Byte == comparision
Understanding Byte and == Op
Byte class(Wrapper class)