• 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

why the outputis false....

 
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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........
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Alexander Hanschke ....
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic