Why Boolean wrapper class implements Serializable interface and Comparable interface ? What is the use of it?
Asked Answered
R

4

1

Why Boolean and Character wrapper classes are implementing Serializable interface and Comparable interface ? What is the use of it?

Ribera answered 15/12, 2012 at 12:59 Comment(3)
What do you mean saying what is the use of it?Cavafy
possible duplicate of Why in java is there a wrapper for every primitive typeMalonis
Look at the javadoc of those two interfaces, and the doc will explain what these interfaces are for.Cussed
S
8

The Comparable interface was added to the Boolean class in Java 5, to address bug JDK-4329937, and at least one other. One of the issues cited was sorting boolean columns in a JTable.

Initially, there was pushback from no less than Joshua Bloch:

The current design is consistent with the language itself: it is a compile-time error to attempt to compare two booleans for order:

if (true < false)  // ERROR: WON'T COMPILE
    foo();

The wrapper class (Boolean) merely mirrors the behavior of the wrapped primitive. ...

We would be willing to sacrifice this "design purity" on the altar of pragmatism, but I'm not convinced that there is a real need for comparing Booleans. It is extraordinarily rare to want to sort a list of Booleans. More common is to want to sort a list of objects containing a Boolean field based on this field, but doing this requires the use of a Comparator. If you're writing a Comparator anyway, it's straightforward to sort based on the Boolean field even though Boolean does not, itself, implement Comparable.

But several years later, the utility was acknowledged:

Over the years it has become apparent that it would make life easier for people if we provided this functionality.

Since this enhancement was implemented, it's become even more useful. For example, in Java 8, the Comparator class introduced new methods comparing() and thenComparing, that can build a comparator based on fields. And it can be reasonable and useful to include a boolean field as part of sort criteria.

Sullyprudhomme answered 2/4, 2021 at 19:18 Comment(1)
Great info, Andy!Offensive
M
5

It implements Serializable so that an object containing it can be serialized. Not making it Serializable would be a serious limitation.

The Comparable isn't so useful as there is only two possible values, so it is likely to be for consistency with other wrappers.

Note: Void is not serializable or comparable, but it can only be null which is serializable.

Miguel answered 15/12, 2012 at 13:58 Comment(0)
O
2

To use any primitive in most of the Collection classes, they have to implement Comparable. Without the wrapper, you couldn't use a primitive in any ordered Collection classes. Also, as a pure primitive, it doesn't have an equals method, so any key based Collection class wouldn't work.

Ontologism answered 15/12, 2012 at 13:7 Comment(0)
M
0

Here's one. Try instantiating ArrayList<T> with a boolean ...

Malonis answered 15/12, 2012 at 13:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.