Java starts from zero for the third day

Source: Internet
Author: User

May 2, 2015 51 just after a day, the computer broke down. Not happy, there is no update

type conversions in Java

Automatic type

In Java programs, it is often necessary to convert data between different basic data types. For example:

The int variable score1 in the code can perform the assignment directly for the double type variable score2, and the result is: 82.0

This conversion is called automatic type conversion .

Of course automatic type conversions are required to meet specific conditions :

1. The target type can be compatible with the source type, such as type double compatible int, but char type cannot be compatible with int

2. The target type is greater than the source type, such as the double type is 8 bytes long, the int type is 4 bytes, so the double type of the variable can directly hold the int type of data, but the reverse is not possible

Forced type conversions in Java

For example, how do you do this when you need to assign the value of a double variable to a variable of type int?

Obviously, this conversion is not automatic! Because the int type has a smaller storage range than a double type. This is done by forcing type conversions .

Syntax:(data type) values

Operation Result:

As you can see, by forcing the type conversion to assign 75.8 to the int variable, the result is 75, and the number is not rounded, but the decimal bit is truncated directly.

See, forced type conversions can result in loss of data Oh , the small partners in the application must be cautious Oh!

Java starts from zero for the third day

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.