Decimals in Java

Source: Internet
Author: User

Decimals in Java are called floating-point numbers

1. There are two types of:

FLOAT: single-precision floating-point number. 4 bytes.

Double: two-precision floating-point number. 8 bytes.

2. Type conversion

Small capacity  -------------------------------> Large capacity   

Byte,short,char << int << long << float << double

The small size of the type will automatically increase the capacity of large, while the large capacity of the small turn, the loss of precision occurs.

as follows, when the integer 127 is assigned to a variable of float and double, the type is automatically promoted and the output is a floating-point number.

 Public class MyTest {    publicstaticvoid  main (string[] args) {        float a=127;         double b=127;        System.out.println (a);        System.out.println (b);    }}

Output Result:

127.0
127.0

3. By default, floating-point number is double type

The following: 127.0 is a double type, so the variable is assigned to the float type, the type is reduced, the accuracy is lost, and an error is given.

 Public class MyTest {    publicstaticvoid  main (string[] args) {        float a=127.0;        System.out.println (a);    }}

If you want to assign a value of 127.0 to a

Can be forced type conversion, written as float a=127.0f;

Decimals in Java

Related Article

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.