wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why the outputis false.... 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why the outputis false...." Watch "why the outputis false...." New topic
Author

why the outputis false....

karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196


output is........
False
127
127


the last two values 127 and 127 are same......
aren't they stored in String constant pool?
(or)
are they returned by some statement like this....
in the toString() method's body...


do all the wrapper classes' toString() method behave in the same way........

KARTHICK.C , SCJP6-93%
(Born to Win)
Alexander Hanschke
Greenhorn

Joined: Jul 19, 2009
Posts: 3
Hi Karthick,

as you already found out, the problem is the toString() method, which is implemented as follows (for the Integer wrapper, which is implicitly called by the Byte wrapper):



Since you create a new instance, the Strings are not held in the constant pool (correct me if I'm wrong).

Regards,
Alex
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14279
    
  21

I don't think you need to know implementation details of the toString() methods of the wrapper classes for the SCJP...

If you really want to know, lookup the file src.zip in your JDK installation directory, and find the source code for class Byte. I looked, and the toString() method of class Byte looks like this:

If you then lookup the method toString(int i, int radix) in class Integer, you'll see that it builds up the string using a character buffer, and in the end calls new String(...). So no, the strings returned by Byte.toString() are not in the string pool. But you will not get any questions on the exam about this specific topic.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196

I don't think you need to know implementation details of the toString() methods of the wrapper classes for the SCJP...

If you really want to know, lookup the file src.zip in your JDK installation directory, and find the source code for class Byte. I looked, and the toString() method of class Byte looks like this:


Jesper Young wrote:
If you then lookup the method toString(int i, int radix) in class Integer, you'll see that it builds up the string using a character buffer, and in the end calls new String(...). So no, the strings returned by Byte.toString() are not in the string pool. But you will not get any questions on the exam about this specific topic.


the code i posted was found in one of the mock question papers posted......
we can answer the above question only if we know that they are created using new operator......that's why i asked you my friend....

thanks a lot for your reply....
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14279
    
  21

karthick chinnathambi wrote:the code i posted was found in one of the mock question papers posted......
we can answer the above question only if we know that they are created using new operator......that's why i asked you my friend....

thanks a lot for your reply....

Ok, but not all mock exams cover exactly what's on the SCJP exam. Some go (much) further than what's on the real SCJP exam.

From which mock exam was this? Please note that you must quote your sources when you copy a question from a book, mock exam or other source.
karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196

Mock Test Question 9

this is the source of that question.............

thanks again dude.........
karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196

thanks Alexander Hanschke ....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why the outputis false....