Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Java Boolean implement Comparable? [closed]

In Java, operators <, >, >= and <= are not defined for the primitive boolean type. However, the corresponding wrapper class Boolean implements Comparable. That is: true > false is an error, but Boolean.TRUE.compareTo(Boolean.FALSE) > 0 is fine.

How come? Did the language designers change their mind? Why keep the incoherent behavior, then?

Although arbitrary, I can think of advantages to having a total order defined for booleans. Are there any disadvantages?

like image 875
João Manuel Rodrigues Avatar asked Apr 02 '21 16:04

João Manuel Rodrigues


People also ask

How to compare two boolean values in Java?

The compare () method of Java Boolean class compares the two Boolean values (x and y) and returns an integer value based on the result of this method. Here, x (first boolean) and y ( second boolean) are two boolean parameters passed which are compared. This method returns an integer value. It returns value 0, if x==y.

What is the difference between Boolean and Boolean expression in Java?

A boolean type is declared with the boolean keyword and can only take the values true or false: However, it is more common to return boolean values from boolean expressions, for conditional testing (see below). A Boolean expression is a Java expression that returns a Boolean value: true or false.

What is the use of compareTo () method in Java?

The compareTo() method of Boolean class is a built in method in Java which is used to compare the given Boolean instance with the current instance. Syntax: Parameters: It takes a Boolean value a as parameter which is to be compared with the current instance.

How do you return a boolean value in Java?

For this, Java has a boolean data type, which can take the values true or false. A boolean type is declared with the boolean keyword and can only take the values true or false: However, it is more common to return boolean values from boolean expressions, for conditional testing (see below).


1 Answers

Programming languages are not mathematical constructs. They are complex projects spanning many years and a lot of different people. As such, they are subjected to opinions, legacy, disagreements, hype cycles, influences of other languages, poor communication, and unfortunately sometimes also to mistakes and stupidity. You could argue that most decisions about a language are in fact arbitrary.

Your question is perfectly valid: why is it like this? Unfortunately without asking people who made the relevant commits how much they can still remember is not really a viable option. So your guess is as good as anybody else's.

It is what it is, but you are entitled to have your own opinion. Sadly, such inconsistencies can be in some cases frustrating to the point when people abandon a language and create a new one. But since computers are physical, limited things, any new language will also be imperfect and opinionated.

If you ask me, having a total ordering on boolean is a good idea - it wouldn't hurt anybody, while it could provide some limited benefit in certain (although very narrow) cases. But there are many more, much much bigger issues with Java. As it stands, I don't think Oracle will risk breaking any existing programs by changing this behaviour.

like image 195
jurez Avatar answered Nov 14 '22 23:11

jurez