Java-Preliminary Understanding-Chapter II-Arithmetic operators 2

Source: Internet
Author: User
Tags arithmetic operators manual writing

One. Describes other types of operators.

In addition to the +,-,*,/,%,+ connector described earlier, there are two + +,--。

Two. + + self-increment

Definition: Add one on the basis of the original data and assign it to the original data.

The self-increment operation was demonstrated and the results were as follows: not familiar with the usage method of + +.

Note: The self-increment operation is not simple, and the same output can be written in two ways. For example: written a++, or ++a can be. There is a profound principle behind this, not so simple as it looks.

This is actually a matter of priority. A++ is a first to participate in the left side of the operation, and then a plus one, and ++a is no matter what the left is, first a plus one, and then a generation into the left of the operation. ++a shows that the increment priority is higher than the arithmetic operator. (Late for the type of operator to dig deeper.) )

Example: a=3; b=a++; The process is, b=a=3, and then a=a+1=4.

A=3; B=++a; The process is, a=a+1=4, then b=a=4;

b*a++ b*a a=a+1

B*++a a+1 B*a

Both a++ and ++a,a are added one after the operation, but the value of the assignment object on the left will change.

In the video tutorial, we have a new understanding of the definition of variables. You can do this without starting the initial value, as long as the variable has a specific value in the final output.

And even if I define the variable, as long as the output statement, no use of the variable, DOS will not error. As follows:

Three. Bottom-up analysis of the a++

with int a=3,b; b=a++; As an example, the analysis is carried out. At first, it is described as follows: A after the other operations (here is the first assignment), then the self-increment. However, in the case of an assignment statement, the right side of the calculation should be completed before the assignment is finally made. Look at the front and back seems to be a contradiction, the correct underlying operation is this, a in the self-increment before the original means to save, and then a self-increment, then the computer will save the original a value assigned to B.

In the following example, it is more illustrative of the problem of sequencing the underlying operation.

Note: While this is the correct calculation order, we already know, but why is the underlying operation of the computer so? What we are talking about right now is how to do the calculations correctly, and there is no reason to say so.

Four. Assignment operators

The simplest thing we have seen before is that there are many extensions: +=,-=,*=,/=,%=.

For example, the function of the extended operator, a+=2; this means a=a+2, which is a shorthand symbol.

The reason for these extensions is that the presence of the assignment operator is actually a source, and we'll look back at the operation of the variables we talked about earlier.

In the above Java source program, we analyze from the first step. BYTE A=3; Here 3 is the int type, assign it to the variable a of byte type, the computer will judge, see this four byte size constant, its true value is in the range of byte, if so, the coercion type casts, thus assigns the value to the byte type variable A. A=a+4, where the byte type variable and the int type constant 4 are added, the type is automatically promoted to the int type, and eventually a result is obtained, but the result is indeterminate, may be greater than the range of byte, or may be less than, so force the result of the int type to be assigned to a byte type. There is a possibility of a missing precision.

In order to eliminate this lack of precision, the extension of the assignment operator is for this birth.

With the form of + =, there is no lack of precision. Why A+=4, and A=a+4 's results are different? A+=4 and A=3, as a result of a judgment, the system automatically carry out a strong transfer assignment statement, is the assignment of the characteristics of the operation. The a=a+4 does not have a strong transition process, which requires manual writing (byte) (a=a+4), and cannot be strongly equivalent to input int to Byte, which is the error statement. The validation results are as follows:

Java-Preliminary Understanding-Chapter II-Arithmetic operators 2

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.