Conversion of data types in Java

Source: Internet
Author: User

The data types in Java are defined at the time of definition, so they cannot be arbitrarily converted to other data types. We can only do a certain amount of processing of the type conversion. There are two types of conversions: automatic type conversion and coercion type conversion.

① automatic conversion of data types:

If you define a data type variable in your program, you want to represent it with a different data type. Java transforms a data type only if the following conditions are true:

1, the data type before conversion is compatible with the converted type, that is, it can only be the same data type, either numeric type, or character type;

2. The converted data type represents a larger range than the type represented before the conversion.

For example, if you convert a short type of variable A to an int type, because both short and int are integer types, then Java automatically converts a short of type A to the int type, which is the legendary "widening conversion" feature in Java. Let's take a look at the code for a type conversion:

    

public class Datetype {
public static void Main (string[] args) {
int x = 30;//defines an integral type variable
Float y = 12.9f;//defines a variable of floating-point type
System.out.println ("x/y=" + (x/y);//Division operation
}

}

The running result of the program is:

x/y=2.3255816

From the output of the program we can find that after the int type and the float type are computed, the result of the output becomes the float type. If it is a constant calculation of two int type, the resulting type is still int

② Casting of data types

With regard to casts of types, it is necessary to do a casting of a data type when the output type of the program is not the type we want. Don't talk nonsense, look at the code first:

    

public class DataType2 {

public static void Main (string[] args) {

float F = 30.2f; Defining floating-point variables
int x = (int) f;//cast to int type
System.out.println ("x=" +x);//output result
}

}

The running result of the program is:

X=30

Conversion of data types 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.