• 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 ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

SJCP question

Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I write the question Please give me the write answer for that .

class X {
public static void main(String[ ] args) {
Boolean b1 = new Boolean( true );
boolean b2 = true;
Boolean b3 = Boolean.valueOf( "FALSE" );
Boolean b4 = Boolean.valueOf( "yes" );

if ( b1 & b2 || b3 | b4)
System.out.println( "Mercury" );
System.out.println( "Mars" );

Options :

a . Mercury
b . Mars
c . Compilation fails
d . Runtime exception

this is the Question i try it on the compiler 1.5 . the correct answer is Mercury.

But I got this form some simulator where the Question answer is as follows :
c . Compilation fails

All constructions for b1 to b4 are valid. b1 is a Boolean object (created through the Boolean wrapper class) with a value of true. b2 is a boolean primitive with value as true. b3 and b4 are both Boolean objects with the same value of false. b3 and b4 are created through the static Boolean valueOf( ) method. This method accepts primitive boolean values or case-insensitive string representation of the boolean values. If you input an unrecongized boolean value or the null literal, the valueOf( ) method will simply return a Boolean object with a false value.

Nevertheless, Boolean objects created through the Boolean wrapper class cannot be used as an expression in a boolean test. A Boolean object is not the same as a boolean primitive. Therefore, the compilation fails at the if clause. Beware of such trick question.

pls any one can reply me for this question what is the correct answer for that .
    Bookmark Topic Watch Topic
  • New Topic