1

I am reading Effective Java and in the first chapter the first example changes a boolean primitive value into a boolean object reference:

public static Boolean valueOf(boolean b) 
{
    return b ? Boolean.TRUE : Boolean.FALSE;
}

My questions are:

  1. What is the different between boolean primitive value and boolean object reference?

  2. What is the reason to do this?

mmmmmm
  • 32,227
  • 27
  • 88
  • 117
Jan Lee
  • 11
  • 1
  • 2
  • 5
    I'm very sure the answer is also in that book, if your read on... So why don't you read on? ;-) – Lukas Eder Dec 28 '11 at 16:02
  • you can check the answer of this question http://stackoverflow.com/questions/4894311/what-is-the-difference-between-boolean-true-and-true-in-java – Adel Boutros Dec 28 '11 at 16:03

5 Answers5

4

You cannot use primitives in generics. You cannot do this:

List<boolean> x;

but you can do this:

List<Boolean> x;
Jesse
  • 3,751
  • 1
  • 21
  • 19
  • That doesn't actually answer the question, though, since the generic type would be a compile-time constant, not something returned from a function. – Dave Newton Dec 28 '11 at 16:09
  • I think it does answer the question, it explains one difference between primitive and boxed booleans, and also explains a purpose for boxing booleans. Those were he questions, am I missing something? – Jesse Dec 28 '11 at 16:19
  • 1
    I suppose we could comment on why the author chose to explicitly return `Boolean.TRUE` and `Boolean.FALSE`, as opposed to `new Boolean(true)` etc. (because `new Boolean(true)` creates unnecessary object instances btw). – Jesse Dec 28 '11 at 16:21
  • `BitSet` would generally be better. – Tom Hawtin - tackline Dec 28 '11 at 16:44
  • It doesn't explain the purpose for the conversion, because the conversion isn't relevant for a generic declaration. Nor is a declaration a reference. What you say is correct, of course, but there's no boxing in your example, nor values. – Dave Newton Dec 28 '11 at 16:45
2

Remember that the primitive boolean has two possible values: true or false. The object Boolean has three: true, false, and null. That is sometimes very useful.

AlanObject
  • 9,613
  • 19
  • 86
  • 142
1

A primitive cannot be used in all contexts. For instance, when used in any of the collection classes, an object type is required. This is mostly done for you, anyway, by auto-boxing. But you should still know about it, or you will get bitten at one point.

Another thing is that an object type can contain null. In cases where you need to differentiate between true, false and unknown, using Boolean can be an easy solution.

Joeri Hendrickx
  • 16,947
  • 4
  • 41
  • 53
0

1> Boolean is wrapper class for boolean

Wrapper classes are used to represent primitive values when an Object is required. Wikipedia

wrapper class is used to apply some methods and calculation, which is not possible using primitive data types.

2> it depends upon situations.

Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
0

I believe the most common uses for Boolean are for use in generic function object and, unfortunately, reflection.

For instance:

boolean exists = java.security.AccessController.doPrivileged(
    new PrivilegedAction<>() {
        public Boolean run() {
            return file.exists();
        }
    }
);

(Probably less boilerplate in Java SE 8.)

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305