• 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

using Boolean objects

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the following code correct:



If it's not correct, please provide a correct example. I'm trying to do something very simple, and maybe I'm overthinking it. But I'm not sure how to use a Boolean.

Thank you.
[ December 12, 2008: Message edited by: Dorcas Rebanha ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code in this "if" statement...



Will never run -- as a Boolean will never return true, when compared to a string. Perhaps, you meant to do this...



Henry
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're using JDK 5 or later, then this:

can also be written like this:

However both these bits of code share another problem with your original code above - they all throw a NullPointerException if authorized is null. (And so far, there's been no code shown setting it to anything, so yes, it's probably null.) So when using a Boolean like this, you generally need to either (a) test the reference before you use it, to see if it's null, or (b) write your code in a way that ensures the variable has been set non-null before you use it. However, both of these are somewhat error prone, as people can forget to check or ensure when they modify the code. Consequently I usually prefer to use a boolean primitive rather than a Boolean object - that way there's no possibility of null. Usually that's a good thing, I think.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The safest way is the following:

Boolean.TRUE is a constant that represents true as a Boolean; there is also Boolean.FALSE.

By putting it first, and calling equals on it instead of the return value of getAuthorized(), you prevent any NullPointerExceptions (NPEs). It's always best to call equals on a constant like Boolean.TRUE or a String constant just to prevent NPEs:
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would agree with Mike Simmonds; just write if (getAuthorised()) . . .

If you get a NullPointerException, that means there is another error somewhere, that you have forgotten to initialise whatever you were supposed to initialise.
By the way: the preferred naming style for that sort of method is "isAuthorised()".
 
Dorcas Rebanha
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all the help! You have given me what I needed, and taught me other stuff, too.
 
reply
    Bookmark Topic Watch Topic
  • New Topic