tags:

views:

735

answers:

10

Hi,

A co-worker asked me to change a signature from using a primitive "boolean" to using a classed "Boolean". He didn't offer a very good explanation why?

Have any of you heard of this and can any of you explain why it matters or doesn't matter?

Edit: He mentioned that it was good practice for public methods.

The use of the field is just a flag that tells me whether to call one flow or another depending on whether it's true or false.

+16  A: 

Actually I tend to err on the side of passing small-b boolean instead, because I know that a primitive boolean is never going to be null. I'm sure there are valid reasons for using the Boolean type, but I'd want to think about its use in each case.

Jim Kiley
Won't you have to deal with boxing and unboxing as well when using `Boolean`?
Adam Robinson
Yeah I know, I think "boolean" is fine especially since it's just a flag for flow control.
ferrari fan
@Adam Robinson - Yes you would (see my answer) which makes this preference even more perplexing to me.
Justin Niessner
@Adam: Less than you'd think. Recent incarnations of Java do a lot of automatic boxing/unboxing. If I have a myFunction(Boolean input), I can pass a raw "true" or "false" to it without having to explicitly create a new Boolean object.
BlairHippo
We're still using 1.4 :(
ferrari fan
I was about to add that this MIGHT be valid if you were attempting to shunt the result into a List<Boolean>, but if you're in 1.4, that possibility is out, too.
Lord Torgamus
Then you may safely add boxing/unboxing to your list of reasons why your co-worker is being silly. :-)
BlairHippo
@BlairHippo: I was referring to the boxing more from a performance perspective. I come from the .NET world, so I already have my auto boxing/unboxing from a syntactical perspective. I don't know if Java is better about it than the CLR is, but boxing can be a bottleneck in the .NET world. Just curious if it's the same in Java.
Adam Robinson
@Adam: Ah. I don't THINK it's a big deal in terms of performance, but I could well be wrong.
BlairHippo
@Adam: Auto(un)boxing can be a performance hit if you use wrapped objects for huge calculations. E.g. adding values in a loop with a sum variable of type `Long` instead of `long`, which leads to one unboxing and one boxing for each iteration.
Christian Semrau
+18  A: 

Is it database-related? If you have a boolean value in a database, it can hold one of THREE values -- true, false, and null. The Boolean object would let you mimic that behavior.

Basically, it's a matter of whether you want to deal with "null" as a potential input value.

BlairHippo
A `null` `boolean` will obviously represent `FileNotFound`.
Tom Hawtin - tackline
+1 when you've got what is secretly a three-valued variable, Boolean is definitely the right way to go.
Jim Kiley
@Jim: actually, when you need to represent a 3-valued variable, I'd argue that Boolean is exactly what you _don't_ want to use. It violates the principle of least surprise -- people expect booleans to have 2 states.
rmeador
+7  A: 

Actually, in general, it's good practice to use regular primitives unless there's a specific reason not to. It will be slightly faster/less wasteful, though you'd need to be moving a lot of objects around for that to really matter.

In response to your edit, I've never heard of it being good practice for public methods. In the often-cited Effective Java by Josh Bloch, there's an entire item "Prefer primitive types to boxed primitives" (item 49, if you can get your hands on a copy). It sounds like your specific case has no reason to favor of using a big-b Boolean, and using objects creates pitfalls like poor interaction with old code that, for example, uses == rather than equals() (which isn't even possible for a primitive).

Lord Torgamus
+2  A: 

I typically try to make use of the primitive boolean wherever possible.

The only possibility that I can think for a developer to want the Boolean class is boxing/unboxing (but I would think you'd want to prevent boxing/unboxing whenever possible rather than encourage it everywhere) and the possibility for null values.

Justin Niessner
+2  A: 

If you control both sides of the interface (i.e. the calling code and the called method) then you should simply be consistent. You actually incur a bit of overhead if you force the compiler to autobox the variable for you.

If the method in question is one of a set of methods with similar signatures, and all others pass an object of some kind in the position where your boolean goes, then using an object rather than a primitive might simply be a matter of being a bit more consistent.

EDIT: Re-reading the question, if that boolean parameter is really just there to control an if (which is exactly what the primitive is there for), then using the object form is simply a waste of CPU time and memory. I can't think of a sensible reason why it should be an object rather than a primitive.

Carl Smotricz
+3  A: 

The main advantage of Boolean over primitive booleans is that they allow you to have a null value. This is particularly effective for return values but can sometimes be used for an "optional" argument in Java.

Make sure your JavaDocs (and code) can deal with the null

Uri
+2  A: 

Never heard of any valid reason to prefer Boolean over boolean.

Given a chance, always stick with primitives. Boolean variables can be null; and thus can introduce an unexpected behavior in your method. Your co-worker may have some specific reasons based on program implementation/logic.

ring bearer
A: 

If you use Boolean, then you can either pass a boolean or a Boolean to the method

public void setX(boolean b) //only takes boolean

public void setX(Boolean b) //takes Boolean or boolean

This is due to the autoboxing of the booleans into the function.

EDIT: The autoboxing only works in 1.5+

Milhous
Good point, but not in 1.4
ferrari fan
+1  A: 

Keep it simple. Using Boolean:

  • adds an extra layer of complexity
  • takes a true/false state boolean and converts it to a true/false/null state variable
  • offers no advantages if used as a logic flag (assuming there is no database interaction as mentioned by BlairHippo)
  • potentially requires additional lines of code to box/unbox booleans in Java 1.4
Chris Knight
A: 

Are you using any kind of data-binding framework in your app? I recently had to deal with a case where a boolean in a model object needed to be bound to a form. The field in the form was required, but we didn't want to default the value to true or false (because it was important for the user to choose the correct value for the specific case, if there was a default, you'd easily get a lot of incorrect values.) However, before the object could be validated, values from the form had to be bound to it. Of course, if the user hadn't selected a value, it would attempt to bind null to the field, and an exception would be thrown during the bind. So we changed it to a Boolean so that null could be bound to the field temporarily, and then validation could report that the field was required.

Nate