Practical tips Java Type conversions and coercion type conversions

Source: Internet
Author: User
Tags integer numeric range variable
Skills | conversion

If you have previous programming experience, you already know that it is quite common to assign a type of value to a variable of another type. If these 2 types are compatible, then Java will automatically convert. For example, it is always possible to assign a value of type int to a variable of type long. However, not all types are compatible, so not all type conversions can be implemented implicitly. For example, there is no definition of converting a double to a byte. Fortunately, it is still possible to obtain a conversion between incompatible types. To achieve this, you must use a cast of coercion, which completes an explicit transformation between two incompatible types. Let's look at automatic type conversions and forced type conversions.

Automatic conversion of 3.9.1 Java

If the following 2 conditions are true, an automatic type conversion (automatic type conversion) is performed when one type of data is assigned to another type variable:

• These 2 types are compatible.

·

The number of destination types is larger than the source type.

Widening conversion (widening conversion) occurs when all the above 2 conditions are met. For example, the scope of an int is larger than the legal range of all the byte types, so you do not require an explicit coercion of a type conversion statement.

For widening conversions, numeric types, including integer and floating-point (floating-point) types, are compatible with each other, but numeric types and character types (char) or Boolean types (Bollean) are incompatible. Character types (char) and Boolean types (Bollean) are also incompatible with each other.

3.9.2 casts of incompatible types

Although automatic type conversion is helpful, it does not meet all the programming needs. For example, what would you do if you needed to assign a value of int to a byte variable? This conversion does not occur automatically because the byte type is smaller than the int type. This conversion is sometimes referred to as "narrowing the conversion" () because you are sure to make the value of the source data type smaller to fit the target data type.

In order to complete the conversion between the two incompatible types, you must make a mandatory type conversion. Coercion type conversion is nothing more than an explicit type transformation. Its general format is as follows:

(target-type) value

where the target type (target-type) specifies the type to convert the specified value to. For example, the following program segment casts the int type to a byte. If the value of the integer is beyond the range of byte, its value will be reduced because of the modulo (integer divided by byte) of the byte value.

       
        
         
        int A;byte b;//. B = (byte) A;
       
        

A different type conversion occurs when a floating-point value is assigned to an integer type: truncation (truncation). You know integers don't have decimal parts. Thus, when a floating-point value is assigned to an integer type, its decimal branch is given away. For example, if you assign a value of 1.23 to an integer, the result value is just 1, and 0.23 is discarded. Of course, if the floating-point value is too large to fit the target integer type, its value will be reduced by modulo the target type domain.

The following program illustrates coercion type conversions:

       
        
         
        Demonstrate Casts.class conversion {public static void main (String args[]) {byte B;int i = 257; double d = 323.142; System.out.println ("Conversion of int to Byte"); b = (byte) i; System.out.println ("I and B" + i + "" + B); System.out.println ("Conversion of double to int."); i = (int) d; System.out.println ("D and I" + D + "" + i); System.out.println ("Conversion of Double to Byte"); b = (byte) D; System.out.println ("D and B" + D + "" + b);}}
       
        

The output of the 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 D B 323.142 67
       
        

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



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.