PackageEquals; Public classIntegerequals { Public Static voidMain (string[] args) {PrintLine (128); Integer a=128; Integer b=128; System.out.println (A==b); System.out.println (A.equals (b)); PrintLine (127); A=127; b=127; System.out.println (A==b); System.out.println (A.equals (b)); PrintLine (-128); A=-128; b=-128; System.out.println (A==b); System.out.println (A.equals (b)); PrintLine (-129); A=-129; b=-129; System.out.println (A==b); System.out.println (A.equals (b)); } Private Static voidPrintLine (intflag) {System.out.println ("========" +flag+ "========"); }}
Output:
========128========falsetrue========127========truetrue========-128======= =truetrue========-129========falsetrue
Reason:
The number between [-128,127] is cached based on reducing the number of object creation times and memory saving considerations.
[-128,127] This range depends on the setting of the Java.lang.Integer.IntegerCache.high parameter.
Private Static class Integercache { private Integercache () {} staticfinal New integer[-( -128) + 127 + 1]; Static { for (int i = 0; i < cache.length; i++) new Integer (i-128); } }
Integer = = with equals "original"