Type conversion in core Java (15th) Java

Source: Internet
Author: User
Automatic type conversion

RunningProgramType conversion is often required. Java will automatically convert the data type under the following circumstances.

    1. The data type before conversion is compatible with the converted data type.
    2. The value range of the converted data type is greater than that before the conversion.

If a-> B, even if the expression range of B is greater than the expression range of A, but if the accuracy of B is not enough, the final result will lose some precision.

The following conversions do not lose precision:

Byte-> short

Short-> int

Char-> int

INT-> long

INT-> double

Float-> double

However, the following may cause loss of precision:

INT-> float

Long-> float

Long-> double

In short, Int-> long-> float-> double, that isFor upward transformation, Java will succeed by default, but it mayPrecision lossHowever, forced conversion is required for downward transformation, and there is still a loss of precision.


When two numbers of different data types are operated, automatic type conversion may occur. The two numbers are converted to the same type before calculation. Note that this type of conversion does not affect the type of two numbers. In the following example, I of the int type and F of the float type are used for addition operations. First, I is converted to the float type and a float type result is obtained by adding the float type to F, then convert it to the double type and assign it to D. After the assignment is complete, the Data Types of I and F will not change.

If the integer type is short or byte, to avoid overflow, Java will automatically convert the short and byte types in the expression to the int type to ensure the correctness of the calculation result, this is also the "extended conversion" function provided by Java. In the following example, if a and B of the byte type are operated, Java converts all of them to the int type before performing the operation. If it is assigned to byte C, compilation errors will occur.

It should be noted that when the number of the same data type is used for calculation, the value assignment type is not sufficient to represent the number,NoAutomatic type conversion occurs. For example, if the number of two int types is added and the int value is not enough, a compilation error occurs.

Package COM. xujin; public class test {public static void main (string... arg) {int I = 2000; float F = 200f; double D = I + F; system. out. println (d); byte A = 10; byte B = 90; // byte c = a + B; // Type Mismatch: cannot convert from int to byteint c = a + B; system. out. println (c); // int e = 2147483648; // The literal 2147483648 of Type Int Is out }}

Forced type conversion

Syntax:

 
Package COM. xujin; public class test {public static void main (string... arg) {Double X =-9.99; int y = (INT) x; //-9, directly cut down the decimal int z = (INT) math. round (x); //-10, returns the nearest integer system. out. println (y); system. out. println (z );}}

Automatic type conversion during heavy load

Automatic type conversion also occurs during function overloading.

For example, if this method (long a, float B) is used to call method (10, 10) without method (int A, int B, integer 10 is automatically converted to the long type. Function overloading. Java will call the most matched method when selecting an overloaded function.

For example:

10 is first converted to the long type. Only method (floata, float B) in the forced conversion or method is converted to the float type.

 
Package COM. xujin; public class test {public static void main (string... arg) {test = new test (); test. med (10, 10); // 2test. med (10, (float) 10); // 3test. med (float) 10, 10); // 1} public void med (float a, long B) {system. out. println ("1");} public void med (long a, long B) {system. out. println ("2");} public void med (long a, float B) {system. out. println ("3 ");}}

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.