0

The JLS 8, chapt 15.21 specifies two concepts named floating point equality test and integer equality test as follows:

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

If the promoted type is float or double, then a floating-point equality test is performed.

Where floating point's defined as follows:

Floating-point equality testing is performed in accordance with the rules of the IEEE 754 standard:

So, we can refer to IEEE 754 in order to describe behavior with floating-point equality. But what about ints? Where the JLS specifies how it performs integer equality test?

asked Jan 1, 2015 at 8:10
3
  • 1
    Do you need a quotation? The test is a trivial comparison of the bit pattern since integer representation is unique. Commented Jan 1, 2015 at 8:14
  • @Henry Yes, I do. I'm looking for a quotation. BTW, the integer representation depends if we use 1's/2's complement or signed magnitude. So, I think it's not exactly true that the integer representation is unique, because there may be two different representations of 0. Am I right? Commented Jan 1, 2015 at 8:16
  • The signed two's complement representation is specified here: docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.2 Commented Jan 1, 2015 at 8:22

1 Answer 1

2

Since int and all other integral types are signed two's complement in Java, you shouldn't worry about having two different ways of representing 0.

Since you were looking for a reference, the JLS §4.2. provides one:

If an integer operator other than a shift operator has at least one operand of type long, then the operation is carried out using 64-bit precision, and the result of the numerical operator is of type long. If the other operand is not long, it is first widened (§5.1.5) to type long by numeric promotion (§5.6).

Otherwise, the operation is carried out using 32-bit precision, and the result of the numerical operator is of type int. If either operand is not an int, it is first widened to type int by numeric promotion.

answered Jan 1, 2015 at 8:22
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.