In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly talks about "what is the difference between Java = and equals". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what's the difference between Java = and equals"!
Java memory knowledge points:
The memory stored by the reference object is the memory address of the reference object, such as 0xa5, 0xa6; constant values stored by basic data types, such as 1, 2, 5. = = compare the contents of storage memory, because it may be a memory address, it may be a constant value, so the results will be confused.
Before learning more about the underlying principles of = = and equals, know how to use it:
= = when there are objects on both sides, compare whether the reference address of the object is the same, otherwise it is all about comparing whether the constant values are the same
There must be an object on the left side of the equals. If the equals method of the object is called, the true returned is equal, and the equals method of the object can be overridden.
The content of the comparison of the equal sign (= =) and the underlying principle of JVM
Before we explain, let's take a look at the example.
Public static void test1 () {Integer a = new Integer (3); Integer b = 3; Integer c = 3; int d = 3; System.out.println (a = = b); / / false System.out.println (a = = d); / / true System.out.println (b = = c); / / true System.out.println (b = = d); / / true} a = = b is the reason for false
Both sides an and b are object types, so what is compared here is whether the reference address of the object is the same. Where an is the space opened in heap memory by the new keyword, and its reference is to the address of that memory space. While b refers to the meta-sharing mode of Integer, that is, when JVM starts, it will instantiate a batch of Integer data for Integer and put it into the cache pool. The range of this data is [- 128127] by default, which can be adjusted by JVM parameters, while Integer that is not in this range is created by new. When Integer data within this scope is used in development through the shared meta pattern, it is obtained directly from the cache pool. Here, Integer bread3 is taken from the cache pool, and its reference address is also the memory space opened up during batch initialization. The two are different, so return false
A = = d is the reason why true
= = d is the basic data type on both sides, and the other side is the packaging type, which causes the other side to unpack automatically, Integer is changed to int, and the conversion method is Integer#intValue before comparing. So the = = has int types on both sides. So the two are equal. This can be better understood by understanding automatic unpacking.
B = = c is the reason why true
= = there are object types on both sides, so addresses are compared. These two objects are created in accordance with the Integer share metadata, so they are obtained from the cache pool [- 128127]. Both reference addresses point to the memory address of the Integer=3 in the cache pool, so they are equal.
B = = d is the reason for true
C on both sides is the basic data type, and the other side is the packaging type. The comparison principle is consistent with a = d.
JVM assembly instruction analysis public static void test1 () Code: 0: new # 3 / / class java/lang/Integer # # open space in heap memory, and its references are put into the stack. Stack [0] = ref (ref is represented as a reference object) 3: dup # # copy the top element of the stack Stack [1] = ref stack [0] = ref 4: iconst_3 # # Stack constant int=3. The reference just opened up points to the constant int=3. Stack [1] = ref_3 stack [0] = ref_3 5: invokespecial # 4 / / Method java/lang/Integer. "": (I) V # # calls the method generated inside JVM, which returns this=stack [0] and leaves the stack, and then stack [0] = ref_3 8: astore_0 # # out-of-stack-> into the slot Put the top element of the stack into slot 0, that is, stack [0]-> salt [0] = ref_3, then empty the stack (here referring to the Operand stack) 9: iconst_3 # # put the int=3 from the constant pool and put it on the top of the stack, that is, stack [0] = 3 10: invokestatic # 5 / / Method java/lang/Integer.valueOf: (I) Ljava/lang/Integer # # call static method Integer#valueOf 13: astore_1 # # unstack-> enter slot 1 salt stack [0] = Integer.valueOf (3). Note the method 14: iconst_3 # # Stack constant 3 Ljava/lang/Integer [0] = 3 15: invokestatic # 5 / / Method java/lang/Integer.valueOf: (I) Ljava/lang/Integer # # call static method Integer#valueOf 18: astore_2 # # unstack-> enter slot 2 salt stack [0] = Integer.valueOf (3). Note the method 19: iconst_3 # # Stack constant 3 stack [0] = 3 20: istore_3 # # out of stack-> into slot 3 Stack [0] = salt [3] = 3 21: getstatic # 6 / / Field java/lang/System.out:Ljava/io/PrintStream # # call printing method 24: aload_0 # # exit slot 0-> Stack salt [0] = stack [0] = ref_3 25: aload_1 # # exit slot 1-> Stack salt [1] = stack [0] = Integer.valueOf (3) Stack [1] = ref_3 26: if_acmpne 33 # # compare the value of the reference type of the top 2 elements in the stack (here the value is the memory address For example, if the result is not equal, jump to 33 29: iconst_1 # # branch 1: if the above instruction does not jump, then continue. Here, print the result directly for the stack int1 30: goto 34 # # jump 34, that is, if the two reference types are different, print the result directly. Otherwise jump 33 33: iconst_0 # # Branch 2: compare the two reference types are equal, then enter the stack int=0, and then print, where the boolean type Actually int type (0,1) 34: invokevirtual # 7 / / Method java/io/PrintStream.println: (Z) V 37: getstatic # 6 / / Field java/lang/System.out:Ljava/io/PrintStream # # first System.out.println (a = = b) End 40: aload_0 / / out of slot 0-> stack salt [0] = stack [0] = ref_3 41: invokevirtual # 8 / / Method java/lang/Integer.intValue: () I # # call static method Integer#intValue 44: iload_3 # # out of stack-> into slot 3 Stack [0] = salt [3] = 3 45: if_icmpne 52 # # compare the value of int type, jump 52 when the result is not equal, otherwise continue 48: iconst_1 # # branch 1: execute when the value is equal, the stack constant 1 stack [0] = 1, here is the same as above Boolean value = true 49: goto 53 52: iconst_0 # # Branch 2: execute when the values are not equal. Enter the constant 0 stack [0] = 0. As above, Boolean value = false 53: invokevirtual # 7 / / Method java/io/PrintStream.println: (Z) V # # second System.out.println (a = = d) End 56: getstatic # 6 / / Field java/lang/System.out:Ljava/io/PrintStream Aload_1 # # exit slot 1-> Stack salt [1] = stack [0] = Integer.valueOf (3) 60: aload_2 # # exit slot 2-> Stack salt [2] = stack [0] = Integer.valueOf (3) Stack [0] = Integer.valueOf (3) 61: if_acmpne 68 # # reference address comparison: 64: iconst_1 # # Branch 1: true 65: goto 69 68: iconst_0 # # Branch 2: false 69: invokevirtual # 7 / / Method java/io/PrintStream.println: (Z) V # # third System.out.println (b = = c) End 72: getstatic # 6 / / Field java/lang/System.out:Ljava/io/PrintStream 75: aload_1 # # out of slot 1-> Stack salt [1] = stack [0] = Integer.valueOf (3) 76: invokevirtual # 8 / / Method java/lang/Integer.intValue: () I call static method Integer#intValue 79: iload_3 # # Out of slot 3-> stack salt [3] = stack [0] = 3 Stack [0] = Integer.valueOf (3). IntValue 80: if_icmpne 87 # # compare the values of int types and jump to 87 when the results are not equal Otherwise continue 83: iconst_1 # # Branch 1: true 84: goto 88 87: iconst_0 # # Branch 2: false 88: invokevirtual # 7 / / Method java/io/PrintStream.println: (Z) V # # fourth System.out.println (b = = d) End 91: return # # method ends the conclusion of the assembly instruction
= = when there are reference types on both sides, the stored value of the reference type (which stores the address of the object to which it points) is directly compared to determine the result
= = when there is a reference type on either side, the reference type is converted to a basic data type (the constant value is stored), and then the result is judged by comparing the numerical value.
The basic data type boxing process (which is also the process of building the wrapper type): load the primitive data constant and call the valueOf method of its corresponding wrapper type to convert the primitive data type to the wrapper type.
Unpacking process of packaging type: call its corresponding xxValue method to get its basic data type constant, such as Integer's intValue
Consider another example public static void test2 () {Integer a = new Integer; Integer b = 150; Integer c = 150; int d = 150; System.out.println (a = = b); / / false System.out.println (a = = d); / / true System.out.println (b = = c); / / false System.out.println (b = = d) / / true} b = = the reason why c is false
= = there are object types on both sides. According to the build of the wrapper type (boxing process), the ValueOf method is called first to instantiate the modified wrapper object.
For Integer
Public static Integer valueOf (int I) {/ / share meta-range data directly returns the cache object if (I > = IntegerCache.low & & I =-128& & l =-128& & sAsInt
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.