wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Boolean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Boolean" Watch "Boolean" New topic
Author

Boolean

krussi rong
Ranch Hand

Joined: Jan 30, 2002
Posts: 62
Hi,
here is the question:
Given these code snippets,
1. Boolean b1=new Boolean(true);
2. Boolean b2=new Boolean(true);
which expressions are legal JAVA expressions that
return true? Select all valid answers.
a. b1==b2
b. b1.equals(b2)
c. b1 & b2
d. b1 || b2
why just b is correct? not C or d?
thanks
Krussi
Francisco A Guimaraes
Ranch Hand

Joined: Mar 20, 2002
Posts: 182
  • c causes a compile error because b1 and b2 are not numeric values
  • d causes a compile error because b1 is Boolean, the wrapper class, and not boolean, the primitive type.


  • Hope your doubt is cleared,
    Francisco
    [ May 28, 2002: Message edited by: Francisco A Guimaraes ]

    Francisco<br />SCJP<br />please use the [code][/code] tags when showing code.Click <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">here</a> to see an example.
    Jessica Sant
    Sheriff

    Joined: Oct 17, 2001
    Posts: 4313

    because you're talking about the WRAPPER class Boolean, not the PRIMITIVE class boolean.
    Therefore you need to compare them like objects --> with the .equals(Object) method "Returns true if and only if the argument is not null and is a Boolean object that represents the same boolean value as this object."
    the equality operator and comparitive operators ==, & and || require the two values to be of the primitive type boolean. If you wanted to do those comparisons with the Wrapper class Boolean you'd need to adjust them to this:
    b1.booleanValue() == b2.booleanValue();
    b1.booleanValue() & b2.booleanValue();
    b1.booleanValue() || b2.booleanValue();


    - Jess
    Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
    bob towers
    Ranch Hand

    Joined: May 20, 2002
    Posts: 33
    I presume you mean:
    b1.booleanValue() && b2.booleanValue();
    regs
    bob


    -------------------------------<br />The early bird gets the worm! So if you want something else for breakfast, stay in bed.<br />-------------------------------
    Gautam Sewani
    Ranch Hand

    Joined: Apr 19, 2002
    Posts: 93
    It can be b1.booleanValue() & b2.booleanValue() as well
    krussi rong
    Ranch Hand

    Joined: Jan 30, 2002
    Posts: 62
    thanks very much!
    Krussi
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Boolean