"Java Puzzles" Java's knowledge of anti-common sense (cont.)

Source: Internet
Author: User


VI, q:


Analysis:In the Boolean expression (i! = 0 && i = =-i), the unary minus operator acts on I, which means that its type must be numeric:Unary minus operator is illegal for a non-numeric operand。 Therefore, we are looking for a numeric value that is not 0, which equals its own negative value.NaN cannot satisfy this property because it is not equal to any numeric value, therefore, I must represent an actual number. Surely no number satisfies such a property? WellThere are no real numbers with this attribute, but none of the Java numeric types can perfectly model real numbers. Floating-point values are represented by a sign bit, a valid number that is popularly known as the Mantissa (Mantissa), and an exponent. In addition to 0, no floating-point number equals the value after the reversal of its sign bit, so the type of I must be integer. The signed integer type uses a complement arithmetic operation of 2: to take a negative value for a number, you reverse each bit and then add 1. A great advantage of the 2 complement arithmetic operation is that 0 has a unique representation. If you want to take a negative value of int value 0, you will get 0xffffffff+1, which is still 0. However, this also has a corresponding disadvantage, a total of an even number of int values (accurately 2^32), one of which is used to represent 0, so that the odd number of int is left to represent positive and negative integers, which meansThe number of positive and negative int values must not be equal. This implies that there is at least one int value whose negative value is not correctly represented as an int value. In fact, there is exactly one such int value, it is integer.min_value, that is, -2^31. Its hexadecimal representation is 0x80000000. Its sign bit is 1, and all the remaining bits are 0. If we have negative values for this value, then we will get 0x7fffffff+1, that is, 0x80000000, which is integer. min_value! Other wordsInteger.min_value is its own negative value, Long.min_value is the same. A negative value on both values will result in an overflow, but Java ignores overflow in integer calculations.


A: the following declaration will make the Boolean expression (i! = 0 && i = =-I) evaluates to true:int i = Integer.min_value; The following can also be:long i = long.min_value;


Summary: If you are familiar with the modulo operation, it is necessary to point out that the puzzle can also be solved by algebraic methods. The int arithmetic operation of Java is the actual arithmetic operation to the 2^32, so this puzzle needs a non-0 solution to this linear congruent:i≡-i (mod 2^32); Add I to both sides of the identity, we can get:2i≡0 (mod 2^32); The non-0 solution to this congruent is i = 2^31. Although this value cannot be represented as an int, it is congruent with -2^31, that is, congruent with Integer.min_value. In short,Java uses 2 of the complement of arithmetic operations, which are asymmetric. For each signed integer type (int, long, byte, and short), the negative value is always one more than the positive value, and the extra value is always the smallest value that this type can represent. a negative value for the integer.min_value is still not changed, and so is long.min_value. to short. Min_value takes a negative value and transforms the resulting int value back to short, returning the same initial values (Short.min_value). For Byte.min_value, similar results can be produced.



Note: This "Java doubts" series are bloggers read the original book "Java Doubts" after the original book on the explanation and examples of the adaptation and then write a blog post. All examples are personally tested and shared on GitHub. Use these examples to motivate yourself to benefit others. At the same time, all the posts in this series will be published in the blogger personal public search "Love Ape" or "ape_it" to facilitate reading. If there is any infringement of the original rights of the author, please inform the blogger in time to delete if the reader has objections to the content of the text or questions are welcome through the blog post or public messages and other ways to discuss together.

Source code Address Https://github.com/rocwinger/java-disabuse


This article from "Winger" blog, declined reprint!

"Java Puzzles" Java's knowledge of anti-common sense (cont.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.