JAVA Data type Overflow analysis __java

Source: Internet
Author: User
Package p20140207;

public class T20140207 {public

	static void Main (string[] args)  
    {  
		int test =integer.max_value;
		System.out.println ("no.1=" +test);
		System.out.println ("no.2=" + (test+1));
		System.out.println ("no.3=" + (test+1l));
    }  
      

Guess what the result would be?


The results are as follows: no.1= 2147483647

No.2=-2147483648

no.3= 2147483648


Package p20140207;

public class T20140207 {public

	static void Main (string[] args)  
    {  
		int test =java.lang.integer.max_value;
		System.out.println ("no.1=" +test);
		System.out.println ("no.2=" + (test+1));
		System.out.println ("no.3=" + (test+1l));
		System.out.println ("no.4=" + (Math.pow (2));//int can represent 2 of 32 times = 4,294,967,296 digits
		System.out.println ("no.5=" + ( Math.pow (2, 32)/2-1))//maximum number of representations
    }  
	//0~2147483647 represent 0~2147483647
	//2147483648~ 4294967295 respectively represents -2147483648~-1
	//So when the 2147483647+1 becomes the No. 2147483648 number, it turns into -2147483648
      
}.


PS: When the operation is to use 2 of the complement of the calculation, the details are not specifically listed.

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.