Java type conversion and forced type conversion

Source: Internet
Author: User
Java type conversion and forced type conversion-general Linux technology-Linux programming and kernel information. The following is a detailed description. If you have previous programming experience, you already know that it is quite common to assign a value of one type to another type of variable. If these two types are compatible, Java will automatically convert them. For example, it is always feasible to assign a value of the int type to a variable of the long type. However, not all types are compatible. Therefore, not all types can be implicitly converted. For example, the double type is not converted to the byte type. Fortunately, conversions between incompatible types are still possible. To achieve this goal, you must use a forced type conversion function to perform Explicit conversions between two incompatible types. Let's take a look at automatic type conversion and forced type conversion.

3.9.1 automatic conversion of Java
If both of the following conditions are met, automatic type conversion is performed when one type of data is assigned to another type variable ):

· 2
Type is compatible.
·
The range of the target type is greater than that of the source type.
Widening conversion occurs when both of the preceding conditions are met. For example, the int type has a larger range than the valid range of all byte types, so explicit forced type conversion statements are not required.

For extended conversion, the numeric types, including integer and floating-point, are compatible with each other. However, the numeric and character types (char) or boolean type (bollean) is incompatible. The char and Boolean types are also incompatible.

3.9.2 forced conversion of incompatible types
Although automatic type conversion is helpful, it cannot meet all programming needs. For example, what if you want to assign an int value to a byte variable? This conversion will not be performed automatically, because the byte type has a smaller change range than the int type. This type of conversion is sometimes called "narrow conversion" (), because you must reduce the value of the source data type to fit the target data type.

To convert two incompatible types, you must perform a forced type conversion. Forced type conversion is only an explicit type conversion. Its common format is as follows:

(Target-type) value

The target type specifies the type to convert the specified value. For example, the following program segment forcibly converts the int type to the byte type. If the value of an integer exceeds the value range of the byte type, the value of the integer is reduced by the modulo of the byte value field (the remainder of the integer divided by the byte value.

Int;
Byte B;
//...
B = (byte);

When the floating point value is assigned to the integer type, a different type conversion occurs: truncation ). You know the integer has no decimal part. In this way, when the floating point value is assigned to the integer type, its fractional part will be removed. For example, if 1.23 is assigned to an integer, only 1 and 0.23 are discarded. Of course, if the floating point value is too large and cannot be suitable for the target Integer type, its value will be reduced due to the modulo of the Value Field of the target type.

The following program illustrates forced type conversion:
// Demonstrate casts.
Class Conversion {

Public static void main (String args []) {
Byte B;
Int I = 257;
Double d = 323.142;

System. out. println ("\ nConversion of int to byte .");
B = (byte) I;
System. out. println ("I and B" + I + "" + B );

System. out. println ("\ nConversion of double to int .");
I = (int) d;
System. out. println ("d and I" + d + "" + I );

System. out. println ("\ nConversion of double to byte .");
B = (byte) d;
System. out. println ("d and B" + d + "" + B );

}
}

The output of this program is as follows:

Conversion of int to byte.
I and B 257 1

Conversion of double to int.
D and I 323.142 323

Conversion of double to byte.
D and B 323.142 67

Let's take a look at each type conversion. When the value 257 is forcibly converted to a byte variable, the result is 257 divided by 256 (256 is the variation range of the byte type) remainder 1. When the variable d is converted to the int type, its fractional part is discarded. When the variable d is converted to the byte type, its fractional part is discarded, and its value is reduced to a modulo of 256, that is, 67.

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.