Comparing variables

Hello,

I started the course Learn Java and have a question in response to exercise 11, about equality operators.
They tell me that “Equality operators do not require that operands share the same ordering. For example, you can test equality across boolean , char , or int data types.”

But when I compare a boolean with a int, like

boolean fish = true;
int monkey = 6 ;
		System.out.println(fish == monkey);

I get an error. It isn’t possible to compare a boolean with another data type. The explanation suggested that it should be possible. Did I read it wrong? Or did I make a mistake in my code?

2 Likes

It seems inlogical to compare a boolean “true or false” with a number… Its like saying yes equals 5… If it does give an output it should atleast be a “false”…

Anyways maybe this link can shed some light on the topic.

1 Like

thanks.
That’s true, it’s a situation that will never occur in practice.

1 Like

I think the lesson’s explanation is off, or at least unclear – I submitted a bug report.

In Java, like you found, you can’t test whether a boolean is equal to something like a char or an int. With booleans, you can basically only check whether they are equal to other booleans.

You can test whether a char is equal to an int, because Java considers both char and int to be numeric types:

The numeric types are the integral types and the floating-point types.

The integral types are byte , short , int , and long , whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two’s-complement integers, respectively, and char , whose values are 16-bit unsigned integers representing UTF-16 code units[.]


Fun fact: the code below will print true:

char myChar = 'A';
int myInt = 65;

System.out.println(myChar == myInt);

You can see why if you look up what, in UTF-16, is the decimal value of the “A” character.

There is no ordinal matching on the string character. How could it possibly match an integer?

1 Like

Not sure what you mean by ordinal matching, but the reason you can test the equality of a char and int is related to their both being numeric types.

These are some relevant bits of the Java language spec:

15.21.1. Numerical Equality Operators == and !=

If the operands of an equality operator are both of numeric type … binary numeric promotion is performed on the operands …

If the promoted type of the operands is int or long , then an integer equality test is performed.

5.6.2. Binary Numeric Promotion

When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order: …

2 . Widening primitive conversion … is applied to convert either or both operands as specified by the following rules: …

[If neither is a double, float, or long], both operands are converted to type int .

So when testing a char and an int for equality, the char will be converted to int, and the two will be compared as ints.

65 is the ordinal for A.

if ord(myChar) == myInt

The above is Python, but we can see the ordinal matching in the comparison.

1 Like