Alibaba Development Manual, (IV) OOP protocol, article 7 explains:
【 Force 】 Compare values of all integer wrapped class objects using equals. Note: For Integer var =? The value of an Integer in the range -128 to 127 is generated in integerCache. cache and will reuse existing objects. The value of an Integer in this range can be determined directly by using ==, but all data outside this range will be generated on the heap. It does not reuse existing objects, which is a big pit and is recommended to use equals.
Take a look at the code below and guess what the result is? Please analyze for 30 seconds to see the result.
public class IntegerTest {
public static void main(String[] args) {
Integer a = 100,
b = 100,
c = 200,
d = 200; System.out.println(a == b); System.out.println(c == d); }}Copy the code
The output
true
false
Copy the code
Are there any surprises? The only time you need an interview question is if you want to cache a number between -128 and 127, but why cache such a small segment? Could there be similar problems with other types of packaging?
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
Copy the code
Integercache.cache if the value of I is greater than -128 and less than 127, the value will be retrieved directly from integerCache.cache. The -xx :AutoBoxCacheMax=
parameter sets the maximum value of the cache, i.e. Integercache.high. So was it a eureka moment? If we are dealing with constant Integer values greater than 127, we can use this parameter to improve the performance of the program.
And then we’ll see what the output is, okay?
true
true
Copy the code
ValueOf = valueOf = valueOf = valueOf = valueOf = valueOf = valueOf = valueOf = valueOf If you still want to dig deeper, you can use IDEA to integrate javap tools and decompile assembly code as follows. We need Settings->Tools->External Tools to add an extension tool
- Program needs to specify the location of javap in your JDK directory
- Arguments is the participation of the extension toolbelt,
-c $FileNameWithoutExtension$.class
Insert Macro… View all associated items - The Working directory is configured to the output directory of the class, including the project directory and package directory, so the content is
$OutputPath$/$FileDirRelativeToSourcepath$
Here’s what follows
/ Library/Java/JavaVirtualMachines jdk1.8.0 _101. JDK/Contents/Home/bin/javap -c IntegerTest. Class Compiled the from"IntegerTest.java" public class com.github.codedrinker.basic.IntegerTest { public com.github.codedrinker.basic.IntegerTest(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."
":()V 4: return Copy the code
public static void main(java.lang.String[]); Code: 0: bipush 100 2: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 5: astore_1 6: bipush 100 8: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 11: astore_2 12: sipush 200 15: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 18: astore_3 19: sipush 200 22: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 25: astore 4 27: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream; 30: aload_1 31: aload_2 32: if_acmpne 39 35: iconst_1 36: goto 40 39: iconst_0 40: invokevirtual #4 // Method java/io/PrintStream.println:(Z)V 43: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream; 46: aload_3 47: aload 4 49: if_acmpne 56 52: iconst_1 53: goto 57 56: iconst_0 57: invokevirtual #4 // Method java/io/PrintStream.println:(Z)V 60: return }
ValueOf = valueOf = valueOf = valueOf = valueOf = valueOf = valueOf = valueOf
By the way, I almost forgot the question, do other encapsulation classes also have this problem? We continue to read the source code to find the same problem with Character, Long, Short? Okay, so decompiling Long is up to you.
Manual links to free download links: https://pan.baidu.com/s/1Sbak5iCFfc2yuUtE0tP_kw extraction code: u5qd
Pay attention to the wechat public number “code maker notes” every day to receive knowledge points.