Consensus--i+++j how to calculate?

Source: Internet
Author: User

This is an interesting calculation, and the 3 plus sign is connected. So, how is it combined? Is it based on: i + (++j), or according to (i++) + J to calculate?

This is not much of a problem in a similar way to C + +, because C + + relies on the hardware structure of the implementation and different environmental outcomes. In Java, however, the result is fixed and unaffected by the hardware environment and platform in which it runs.

In what order to calculate, write a program to test it.

Combination of "example" + +
 

If combined as:
  

The value of J is added 1, and the value of the result K is 31. The results of the program run as follows:
  

From the results, the order of Union is the former, that is to say
  

Equivalent:

For clarity, parentheses are used here.

  Greedy rules

This binding is rooted in the greedy rules of the compiler, which means that when parsing symbols, the compiler will combine as many valid symbols as possible, such as the expression above:

"+" and "+ +" are valid symbols, but "+ + +" is not a valid symbol, so after analysis, the expression is resolved to:

However, the compiler is extremely "greedy" by combining as many valid symbols as possible, regardless of whether this combination conforms to the rules of the grammar, such as an expression:

Originally, we originally intended to be a minus? B, namely:

However, if you do not use spaces (or other white space separators that conform to grammatical rules) or parentheses, after parsing, because the symbol "–" is a valid symbol, it is grouped together so that the structure is divided into:

This, of course, does not conform to grammatical rules, so the compiler will ruthlessly generate compilation errors.

  Why greed?

Perhaps the reader will ask: Why use this greedy way to analyze characters? So what good is this? believe that you have seen the following example, you will understand.

"Example" the greedy explanation.

The program is to output several octal escape characters, seemingly not related to the greedy rules at all. If you think so, then you are wrong, see the results of the operation:

Consider why the length of the String object "\17" is 1, and the length of "\171" is 1? Also, why "\171" does not print out the "\17" escape character "" and connects to "1":

Instead, print out:

? That's because of the greedy rules, the compiler will combine as many valid characters as possible, otherwise the escape character will lose its effect. For "\1717" and "\431", it is handled by two characters, because both values exceed the range (\0~\377) of the octal escape character, so \1717 resolves to \171 and 7, and the emphasis content and \431 resolves to "\43" and "1".

  Summarize:

When parsing characters, the compiler combines as many valid characters as possible. and "excessive" greed, regardless of whether this combination is in line with grammatical rules.

Greedy rules are useful because they allow for special handling of escape characters.
  
  
This article is from Lemon pie http://www.lemonpai.com Please keep this source, otherwise you will be held liable!

Consensus--i+++j how to calculate?

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.