Java technology _java Q (0047) _java How basic data types are converted

Source: Internet
Author: User

Click to enter _ more _java thousand ask

1. How the basic types convert to each other

As we can see, it is common to assign one type of value to another type. In Java, it is clear that the Boolean type cannot be converted from all other 7 types.
For other 7 numeric types, they can be converted between them, but there may be a loss of precision or some other change.
8 basic data types in Java see here: what 8 basic data types are in Java
Conversions are divided into automatic conversions and casts. For an automatic conversion (implicit), no action is required, and the coercion of type conversions requires an explicit conversion, that is, using the conversion operator (type).

Start by arranging the 7 types in the following order:

byte < (SHORT=CHAR) < int < Long < float < double

If you convert from small to large, you can do it automatically, and from large to small, you must cast. The same type of short and char two must also be cast.

2. How to convert the type automatically

Widening (widening conversion) occurs during automatic conversion. Because large types (such as int) want to hold smaller types (such as Byte), memory is always sufficient and does not require casting.
If you save literals to Byte, short, char, long, type conversions are also done automatically.
Note the difference, at this point, from int (integer literal with no l as int) to Byte/short/char is also auto-completed, although they are smaller than int. In automatic type conversion, the loss of precision can not occur in other conversions except in the following cases, which may result in loss of precision.
1. int–> Float
2. Long–> Float
3. Long–> Double
4. float–> double without strictfp (unsigned double)
In addition to the possible loss of precision, automatic conversion does not occur with any runtime (run-time) exceptions.

3. How to cast a type

If you want to turn large into small, or convert between short and char, you must cast, also known as a narrowing conversion (narrowing conversion).
Because the numeric value must be explicitly smaller to accommodate the target type. The cast takes the conversion operator (type). Strictly speaking, converting byte to char is not a narrowing conversion because the process from byte to char is actually byte–>int–>char, so both widening and narrowing have.

In addition to the possible loss of precision, the cast can also cause the modulus (overall magnitude) to change. The cast format is as follows:

target-type val =(target-type) value;

For example:

int a=80;  byte b;   b = (byte)a;

But you, if the value of an integer exceeds the range that byte can represent, the result will be the remainder of the range of type Byte. For example:

int a=257;  byte b;   b = (byte)a;System.out.println(b);

The results are as follows:
1

Because a=257 exceeds the range of byte [-128,127], the remainder is obtained by dividing 257 by the range of Byte (256): B=1. It is important to note that when a=200, the remainder should be 56 instead of 200 at this time except for 256.

A truncated (truncation) occurs when a floating-point type is assigned to an integer type. That is, the fractional part is removed, leaving only the integer part. At this point, if the integer exceeds the target type range, the range of the target type will be the remainder.

Java technology _java Q (0047) _java How basic data types are converted

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.