Java-basic data type conversion

Source: Internet
Author: User

Package CN. zhao. base; </P> <p>/** <br/> * data type conversion <br/> **/<br/> public class mydemo <br/> {<br/> Public static void main (string [] ARGs) <br/> {<br/> // six normal sequence type conversions <br/> // byte-> short <br/> // short-> int <br // int-> long <br/> // char-> int <br/> // int-> double <br/> // float-> double </ p> <p> // conversion of precision may be lost <br/> // int-> float <br/> // long-> double <br/>/ /long-> float </P> <p> system. out. printl N ("when two numeric operations are performed, if one is double, the other is also converted to double"); <br/> system. out. println (3 + 3.0); </P> <p> system. out. println ("when two numeric operations are performed, if one is float, the other is converted to float"); <br/> system. out. println (3 + 3.0f); </P> <p> system. out. println ("when two numeric operations are performed, if one is long, the other is also converted to long"); <br/> system. out. println (3l + 3); </P> <p> system. out. println ("Otherwise, both numbers will be converted to int. Obviously, the result is incorrect because 200 is beyond the byte range and the precision is lost."); <br/> system. out. println (100 + (byte) (200); </P> <p> system. out. println ("------------------ forced type conversion -----------------"); <br/> system. out. println ("Double forced int conversion"); <br/> double num = 300.5; <br/> int intnum = (INT) num; <br/> system. out. println (intnum); <br/> // The result is 300. If the decimal point is converted to an integer, the decimal part is removed. <Br/> // if you want to perform exact conversion, you must use rounding. <br/> int intnumround = (INT) math. round (Num); <br/> system. out. println (intnumround); // 301 <br/>}< br/>

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.