Java-Preliminary Understanding-Chapter II-type operation details

Source: Internet
Author: User

I. In-depth exploration of the underlying principles behind variables in different processes

Variables involve two processes, definitions and calculations in the program. Let's give some examples to illustrate the rationale behind it. (in addition to numeric variables, other types can be variables, as long as the variable is always changing.) )

In the process of defining a variable, byte b=4; 4 The default is the int type, assign it to the byte type, in DOS, the error should be the missing precision. But DOS did not error, but the operation is normal. The principle is: in the process of assignment, the computer will determine whether the assigned value is within the specified type range. Here 4 is in the range of the -128~127, as long as within the scope of this, the computer will automatically be forced to type conversion, because it does not cause the loss of precision. Similarly, byte b=4; b=3+7; Results will not be error, the principle is the same.

Note : The same value, in other words, DOS will be an error. Byte b=4, b1=3, b2=7; B=B1+B2; The reason for the error here is that B1 and B2 are variables, with the current 3 and 7来 looking, the result is no loss of precision. But the characteristics of the variable is that the data will change, in order to prevent future out of range, here directly error. → It seems that this argument is somewhat farfetched. To be more clear, the B1 and B2 on the right are uncertain, and the result of the addition of the two may exceed the range of byte. If the type of B is defined here as Int,int can hold all ranges of two byte type variables, no error will be expected. (In fact, the computer is a step-by-step operation, B=B1+B2, my understanding is this, the right first calculation, the result of the calculation is assigned to B, the right side of the result of the calculation is an unknown type before the assignment is a value, according to the principle of Java, the default is int, The value of this int type may be less than the byte range, or it may be greater than byte, so that the precision may be lost when casting. The same principle, for the following example, three int type, X X1 X2, the right side of the undefined, the default is int, if the int range is exceeded, the first discard, so that the assignment to the left int X there is no problem. )

A different type, the operation between three variables, according to Byte, this should be an error, but not. The reason is that, in the computer, any integer operation of the results are still integers, even if the accuracy is exceeded, the system will automatically intercept, so the default type will not error, but the output is not necessarily correct, unless the two participating in the calculation of the int type values are small, not ultra-precision.

Java-Preliminary Understanding-Chapter II-type operation details

Related Article

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.