thinking of = = and equals comparison of Java Integer ( -128~127) value __java

Source: Internet
Author: User

Recently encountered a problem in the project, two values of the same integer value for = = comparison, found some of the mysteries of the integer, by the way also review the difference between = = and equals, first through the Damo code explained as follows:

System.out.println ("integer x = value) in the value of <-128~127 within." > ");
Integer i = 127;
Integer j = 127;
System.out.println ("i=" + i + ", j =" + j);
System.out.println ("i = = J:" + (i = = j) + "<--comparison-->i.equals (j):" + i.equals (j));
System.out.println ("An integer value other than <-128~127, integer x = value," is assigned the way.) > ");
Integer m = 128;
Integer n = 128;
System.out.println ("m=" + M + ", n =" + N);
System.out.println ("M = = N:" + (M = = N) + "<--comparison-->m.equals (n):" + m.equals (n));
System.out.println ();
		
System.out.println ("< arbitrary integer value, integer x = new Integer (value), the way to assign a value." > ");
Integer x = new Integer (299);
Integer y = new integer (299);
System.out.println ("x=" + x + ", y =" + y);
System.out.println ("x = = y:" + (x = = y) + "<--comparison-->x.equals (y):" + x.equals (y));


The output results are:

Integer x = value in the <-128~127 within the value. >
i=127,j =127
i = = j:true<--comparison-->i.equals (j):
integer value other than true <-128~127, integer x = value; The way you assign the value. >
m=128,n =128
m = = n:false<--comparison-->m.equals (n): True


< arbitrary integer value, integer x = new Integer (value ), the way to assign a value. >
x=299,y =299
x = = y:false<--comparison-->x.equals (y): True


Through the above code and output results, we must have seen the mystery. The first is summed up as follows:
1. The first and second paragraphs of the above code are intended to indicate that the integer value that is assigned in the -128~127 integer value and in integer x = value will return True when the = = and equals are compared. Because Java inside the -128~127 between the integer value, with the primary data type int, will be used in memory for reuse, that is, the integer value for = = comparison is only for the INT native data type numeric comparison, and beyond the -128~127 range , the address and numerical comparison are performed when the = = comparison is made.


2, the third paragraph to explain: = = and equals difference, = = is the address and value comparison, can not be overloaded with = = operator, and for the Equals method, Integer inside the Equals method rewrite object Equals method, View the integer source can see that the Equals method is a numerical comparison.

continue to explain:

  First look at a piece of code (using JDK 5) as follows: [HTML]   View plain copy public class hello     {      public static void main (String[] args)        {        int a = 1000, b =  1000;        system.out.println (a == b);            Integer c = 1000, d = 1000;        system.out.println (c == d);            Integer e = 100, f = 100;         System.out.println (e == f);      }   }   

Output result: [HTML] view plain copy true false

The Java Language specification, 3rd Edition wrote: [HTML] view plain Copy to save memory, for two instances of the following wrapper objects, when their basic values are the same , they are always = =: Boolean Byte Character, \u0000-\u007f (7f is a decimal 127) Integer,-128-127

View the JDK source code as follows:[Java]  View plain copy/**       * Cache to support the  object identity semantics of autoboxing for values between        * -128 and 127  (inclusive)  as required by  JLS.       *       * The cache  is initialized on first usage. during vm initialization the       * getAndRemoveCacheProperties method may be used  to get and remove any system       * properites  that configure the cache size. At this time, the size  of the       * cache may be controlled by the  vm option -xx:autoboxcachemax=<size>.       */           // value of java.lang.Integer.IntegerCache.high property  (obtained  During vm init)        private static String  integercachehighpropvalue;          static void  Getandremovecacheproperties ()  {           if  (! Sun.misc.VM.isBooted ())  {                properties props = system.getproperties ();                integerCacheHighPropValue =                     (String) Props.remove (" Java.lang.Integer.IntegerCache.high ");   &NBsp;           if  (integerCacheHighPropValue ! = null)                     system.setproperties (props);  // remove from system props   

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.