• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why the outputis false....

 
karthick chinnathambi
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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........
 
Alexander Hanschke
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
karthick chinnathambi
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mock Test Question 9

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

thanks again dude.........
 
karthick chinnathambi
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Alexander Hanschke ....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic