Decrypting a type conversion problem in Java

Source: Internet
Author: User

well known data types in Java are strong data types, conversions between basic data types are especially fixed, and when data types with narrow data widths (such as int) are converted to data typeswith a wide data type (such as double), The narrow data type is widened to complete the automatic type conversion, which is referred to as an implicit conversion.

such as: The following code does not have any problems, the result is correct, the results will not change, the difference is the accuracy of the results improved.

Intintscore = 96;doubledoublescore = Intscore;

If you attempt to convert a wide data type (such as a double) into a narrow data type (such as float), the compiler will prompt a compilation error, and you want to compile the pass, forcing the type conversion. Then, the data at this time is truncated. The resulting result is:

1. The data is correct, but the accuracy is reduced;

2. Data is incorrect, overflow occurred;

for the above 1 cases, a good understanding, we look at an example:

float floatweight= 63.5; Compilation error double doubleweight= 63.5;

The first statement above prompts for a compilation error, because the compiler sees 63.5 as a double type and assigns a double type to the float type. Of course, the compilation does not pass. The recommended solutions are:

float floatweight = 63.5f;

of course, you can also cast to float type:

float floatweight = (float) 63.5;

in fact, the essence of the above statement is to cast a double type of data into the float type, a truncation occurred. Although the size of the data has not changed, the accuracy of the data has been reduced.

Similarly:

Doubleweight = Floatweight; Implicit conversion floatweight = (float) doubleweight;//cast

Now, the question is, since it is truncated, how can it produce overflow? Let's look at an example:

Shorti = 150;SHORTJ = 75;byteb = (byte) i; byted = (byte) j; System.out.println ("b =" + B); System.out.println ("D =" + D);

The output of the above code is:

b =-106

D = 75

See the result, inevitably ask why b=-106 it? This is because an overflow occurs when casting. Because it exceeds the maximum range that byte can represent (-128 ~ 127).

so -106 and how did it come about?

i =.in binary notationi = 0000 0000 1001 0110, Shorttype accounted for2bytes, -bit, the highest bit of0represents a positive number. When theIforced to convert intobytetype, the high position is truncated,i = 1001 0110. In the computer, the highest bit is indicated by the complement of1indicates a negative number, then it is expressed in the original code:i = 1110 1010, which is exactly the decimal number-106.

This explains why truncation can also occur.


This article is from the "Heart Fly" blog, please be sure to keep this source http://weiweili.blog.51cto.com/6675651/1901017

Decrypting a type conversion problem in Java

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.