Java===java Basic Learning (3)---Data type conversions, operator levels, enumeration types

Source: Internet
Author: User

Data type conversions:

Sometimes, a program needs to have a data type, such as int + float, and the result is a float, where int is converted to float type, which is a legal conversion.

The legitimate transformations in Java are as follows:

Red indicates no loss of information for the conversion, and Orange indicates a potential loss of progress.

 PackageTestbotoo; Public classShuzhileixingzhuanhuan { Public Static voidMain (string[] args) {intn = 123456789; floatf = n;//convert int type to float typeSystem.out.println (f);        //Forcing type conversions        Doublex = 9.9997; intNX = (int) x;//Strong turn operation, so that you can truncate the decimal part NX = 9//Of Course we can also perform rounding operations, using the Math.Round () method        DoubleX1 = 9.9997; intNX1 = (int) Math.Round (x1);                System.out.println (NX1); //Operation LevelA && B | | C equivalent to (a && b) | |c a+ = b + = c equivalent to A + = (b + =c) Operation level as shown in:}}

Conversion rules:

* If one of the two operands is a double type, the other operand is converted to a double type
* Otherwise, if one of the operands is of type float, the other operand will be converted to float type
* Otherwise, if one of the operands is of type long, the other operand will be converted to a long type
* Otherwise two operands will be converted to int type

Excerpt from: "Java Core technology" 3.5.5

Forced swap:

double x = 9.9997; int NX = (int// Strong turn operation, so it can truncate the decimal part NX = 9        // Of course we can also perform rounding operations, Used is the Math.Round () method Double x1 = 9.9997; int nx1 = (int) math.round (x1); System.out.println (NX1);

The level of the operator:

        A && B | | C equivalent to  (a && b) | | C         + = b + = c equivalent to   a + = (b + = c)

As shown in the following:

Java===java Basic Learning (3)---Data type conversions, operator levels, enumeration types

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.