If I want to declare a boolean array, I used to do this:
boolean[] B = new boolean[n];
all the element in array is false
Why couldn't do this?
Boolean[] B = new Boolean[n];
I knew boolean is primitive type while Boolean is its wrapper class. Why it's not like you declare ArrayList, here you use wrapper class instead of primitive class?
The difference
A Boolean[] is an array of references to Boolean objects. This means that index i will always be one of the following
array[i] == null
array[i] == Boolean.TRUE
array[i] == Boolean.FALSE
A boolean[] on the other hand, is an array of primitives, which means that you'll always have one of
array[i] == true
array[i] == false
Comparing to ArrayList<Boolean>
Why it's not like you declare ArrayList, here you use wrapper class instead of primitive class?
This is because generics were not designed to handle primitives, so you're forced to use the boxed versions.
This might change in future versions of Java. Here's a writeup from Brian Goetz on the subject:
State of the Specialization
See also:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With