This week's book giveaway is in the Design forum.
We're giving away four copies of Experimentation for Engineers: From A/B testing to Bayesian optimization and have David Sweet on-line!
See this thread for details.
  • 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
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Boolean

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
  • 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 ]
     
    Sheriff
    Posts: 4313
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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();
     
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I presume you mean:
    b1.booleanValue() && b2.booleanValue();
    regs
    bob
     
    Ranch Hand
    Posts: 93
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    It can be b1.booleanValue() & b2.booleanValue() as well
     
    krussi rong
    Ranch Hand
    Posts: 62
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    thanks very much!
    Krussi
     
    I suggest huckleberry pie. But the only thing on the gluten free menu is this tiny ad:
    The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
    https://www.kickstarter.com/projects/paulwheaton/low-tech
    reply
      Bookmark Topic Watch Topic
    • New Topic