Java Foundation---Forced type conversion in Java (eight)

Source: Internet
Author: User

Forced type conversions in Java

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

650) this.width=650; "src=" http://img.mukewang.com/53688a9a0001f73404330109.jpg "/>

Run Result: 650) this.width=650; "src=" http://img.mukewang.com/5359d73a00016cde00500033.jpg "/> can be seen by forcing type conversion to assign a value of 75.8 to After the int variable, the result is 75, 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!


Code:

public class helloworld{
public static void Main (string[] args) {
Double heightavg1=176.2;
int heightavg2= (int) heightAvg1;
System.out.println (HEIGHTAVG1);
System.out.println (HEIGHTAVG2);
}
}

Operation Result:

176.2
176

1. Coercion type conversion syntax: (data type) numeric value

2. Note the use of English symbols


This article is from "Ghost" blog, please make sure to keep this source http://caizi.blog.51cto.com/5234706/1547644

Java Foundation---Forced type conversion in Java (eight)

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.