Question 86: Toxic brackets

Source: Internet
Author: User

Can you name such a legal Java expression? You only need to add brackets to a subexpression to make it an invalid expression, the brackets are added only to indicate the order of values when no brackets are added?

Inserting a pair of parentheses to annotate the existing assignment order does not seem to have any impact on the validity of the program. In fact, in most cases, there is no impact. However, in both cases, inserting a pair of parentheses that appear to be unaffected may make legal Java programs invalid. This strange situation is caused by the asymmetry of the binary complement of values, as discussed in puzzles 33 and 64. You may think that the absolute value of the smallest int type negative number is 1: integer greater than the largest int type positive number. min_value is-231, that is,-2,147,483,648, while integer. max_value is 231-1, that is, 2,147,483,647.

Java does not support negative decimal literal constants. Negative constants of the int and long types are composed of negative operators (-) before the decimal literal constants. This construction method is determined by a special language rule: the maximum value of a decimal literal constant of the int type is 2147483648. All decimal literal constants ranging from 0 to 2147483647 can appear anywhere where int-type literal constants can be used, however, the literal constant 2147483648 can only be used as the operand of the negative unary operator [JLS 3.10.1].

Once you know the rule, this puzzle is easy. Symbol-2147483648 constitutes a valid Java expression, which consists of a negative unary operator plus an int-type literal constant 2147483648. By adding a pair of parentheses to annotate (not important) The assignment order, that is, writing-(2147483648) will destroy this rule. Believe it or not, the following program will certainly have a compilation error. If the brackets are removed, the error will be gone:

public class PoisonParen {    int i = -(2147483648);}

Similarly, the preceding situation applies to long literal constants. The following program will also generate a compilation error and disappear if you remove the brackets:

public class PoisonParen {    long j = -(9223372036854774808L);}

Question 86: Toxic brackets

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.