• 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

what is the output of this

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code will print
1: if( new Boolean("true") == new Boolean("true"))
2: System.out.println("True");3: else
4: System.out.println("False");A) Compilation error.
B) No compilation error, but runtime exception.C) Prints "True".
D) Prints "False".
 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi surya,
It will print out False, so the answer is D. The reason for this is that you are using the equals operator (==) to test two objects. In this case, the operator tests to see if the two objects denote the same object. If this test was on two boolean primitive types, the answer would be true.
/rick
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Equality operators when applied to reference types
is explained in JLS 15.21.3
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#236163
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"surya"
Please review the JavaRanch Naming Policy carefully then re-register with a Display Name that conforms with the policy.
Thanks for your cooperation.
 
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, if you want to know what the output of a piece of code will be why not compile it and see for yourself.
If you don't understand WHY it gives the output it does then feel free to come here and ask us. Then it at least proves that you are making some effort to find things out for yourself; we're always happy to help but we have learned that the best way to learn Java is to write code and try things out for yourself.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! 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