Web-android Engineer first form forced type conversion in -2-10 Java

Source: Internet
Author: User

Source: http://www.imooc.com/code/1241

I believe that the small partners have found that, although automatic type conversion is very convenient, but does not meet all the programming needs.

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

Run Result: You can see that by forcing the type conversion to assign 75.8 to the int variable, the result is 75, the value 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!

Task

Practice time again, together to do a blank question!

In the editor, two variables are defined, fill in the Incomplete part of Line 4, and cast the average height to an integer value.

Operation Result:

1  Public classhelloworld{2      Public Static voidMain (string[] args) {3         Doubleheightavg1=176.2;4         intHeightavg2=5 System.out.println (HEIGHTAVG1);6 System.out.println (HEIGHTAVG2);7     }8}

--------------------------------------------------------------------------------------------------------------- -----------

1  Public classhelloworld{2      Public Static voidMain (string[] args) {3         Doubleheightavg1=176.2;4         intHeightavg2= (int) HeightAvg1;5 System.out.println (HEIGHTAVG1);6 System.out.println (HEIGHTAVG2);7     }8}

Web-android Engineer first form forced type conversion in -2-10 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.