[JAVA,2017-05-17] Data type parameter amusing

Source: Internet
Author: User

What is the maximum value of int? What about a plus? by 2?

The first question I think most people know is that I don't know how many people have studied the latter two.

First, the previous section of code:

1  Public classDecimaltest {2      Public Static voidMain (string[] args) {3System.out.println ("int==============");4         intint_max=Integer.max_value;5         intint_min=Integer.min_value;6System.out.println ("int_max===" +Int_max));7System.out.println ("int_max+1===" + (int_max+1)));8System.out.println ("int_min===" +int_min));9System.out.println ("int_min-1===" + (int_min-1)));TenSystem.out.println ("int_max*2===" +int_max*2)); OneSystem.out.println ("int_min*2===" +int_min*2)); A          -             the     } *}

Run results

int==============
int_max===2147483647
int_max+1===-2147483648
int_min===-2147483648
int_min-1===2147483647
Int_max*2===-2
Int_min*2===0

As we can see, the maximum value of int is changed to the minimum after 2, then to 2 when the minimum value of int becomes the maximum after minus one, and the value becomes 0 after 2.

This is because the value of Int_max is represented by a binary representation of 01111111111111111111111111111111, plus one for 10000000000000000000000000000000,

And 10000000000000000000000000000000 is the complement of Int_min's binary form.

So the maximum value plus one becomes the minimum value, and the minimum value becomes the maximum value minus one.

In addition, there are other data types such as short, long, and so on.

System.out.println ("short=============");  Shortshort_max=Short.max_value;  Shortshort_min=Short.min_value; System.out.println (("short_max===" +Short_max)); System.out.println (("short_max+1===" + (short_max+1))); System.out.println (("short_min===" +short_min)); System.out.println (("short_min-1===" + (short_min-1))); System.out.println (("short_max*2===" +short_max*2)); System.out.println (("short_min*2===" +short_min*2)); Longlong_max=Long.max_value; Longlong_min=Long.min_value; System.out.println (("long_max===" +Long_max)); System.out.println (("long_max+1===" + (long_max+1))); System.out.println (("long_min===" +long_min)); System.out.println (("long_min-1===" + (long_min-1))); System.out.println (("long_max*2===" +long_max*2)); System.out.println (("long_min*2===" +long_min*2)); 

There are also different types of float and double

System.out.println ("float============="); floatfloat_max=Float.max_value; floatfloat_min=Float.min_value; System.out.println (("float_max===" +Float_max)); System.out.println (("float_max+1===" + (float_max+1))); System.out.println (("float_min===" +float_min)); System.out.println (("float_min-1===" + (float_min-1))); System.out.println (("float_max*2===" +float_max*2)); System.out.println (("float_min*2===" +float_min*2)); System.out.println (("float_min/2===" +FLOAT_MIN/2)); System.out.println ("double============="); Doubledouble_max=Double.max_value; Doubledouble_min=Double.min_value; System.out.println (("double_max===" +Double_max)); System.out.println (("double_max+1===" + (double_max+1))); System.out.println (("double_min===" +double_min)); System.out.println (("double_min-1===" + (double_min-1))); System.out.println (("double_max*2===" +double_max*2)); System.out.println (("double_min*2===" +double_min*2)); System.out.println (("double_min/2===" +DOUBLE_MIN/2));

Results

float=============
Float_max===3.4028235e38
Float_max+1===3.4028235e38
Float_min===1.4e-45
float_min-1===-1.0
Float_max*2===infinity
Float_min*2===2.8e-45
float_min/2===0.0
double=============
double_max===1.7976931348623157e308
double_max+1===1.7976931348623157e308
double_min===4.9e-324
double_min-1===-1.0
Double_max*2===infinity
double_min*2===1.0e-323
double_min/2===0.0

Add one does not change, by 2 to get an Infiniti (Infinity).

Plus one does not change because the calculation of floating-point numbers is not accurate, when the 2 number is very different, the small number will be ignored (personal view, no proof, not sure);

Multiply by 2 for the specific reasons for infinity is not known.

[JAVA,2017-05-17] Data type parameter amusing

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.