What are the results of comparing primitive types and wrapper class objects using == and equals?

1. If the values are different, comparison using == and equals() will return false

2. The values are the same

Use == to compare:

  • Basic type – basic type, basic type – wrapped object returns true
  • Packaging object – packaging object, if it is not the same object (the memory address of the object is different), it returns false; if the memory address of the object is the same, it returns true. The following two Integer objects whose values are equal to 100 ( The reason is that the JVM caches some commonly used wrapper class objects of basic types, such as Integer -128 ~ 127 are cached)
Integer i1 = 100;
 Integer i2 = 100;
 Integer i3 = 200;
 Integer i4 = 200;
         
 System.out.println(i1==i2); //Print true
 System.out.println(i3==i4); //Print false

Compare using equals()

  • Wrapper object – basic type returns true
  • Wrapper object – the wrapper object returns true

3. Compare different types of objects, return false

JDK1.8, experimental code

byte b1 = 127;
Byte b2 = new Byte("127");
Byte b3 = new Byte("127");
System.out.println("Byte primitive types and wrapped objects use == comparison: " + (b1 == b2));
System.out.println("Byte primitive types and wrapped objects are compared using equals: " + b2.equals(b1));
System.out.println("Byte wrapper objects and wrapper objects use == comparison: " + (b2 == b3));
System.out.println("Byte wrapper object and wrapper object use equals to compare: " + b2.equals(b3));
System.out.println();

short s1 = 12;
Short s2 = new Short("12");
Short s3 = new Short("12");
System.out.println("Short primitive types and wrapped objects use == comparison: " + (s1 == s2));
System.out.println("Short primitive types and wrapped objects are compared using equals: " + s2.equals(s1));
System.out.println("Short wrapper objects and wrapper objects are compared using ==: " + (s2 == s3));
System.out.println("Short wrapper object and wrapper object are compared using equals: " + s2.equals(s3));
System.out.println();

char c1 = 'A';
Character c2 = new Character('A');
Character c3 = new Character('A');
System.out.println("Character primitive types and wrapped objects use == comparison: " + (c1 == c2));
System.out.println("Character primitive types and wrapped objects are compared using equals: " + c2.equals(c1));
System.out.println("Character wrapper object and wrapper object use == comparison: " + (c2 == c3));
System.out.println("Character wrapper object and wrapper object use equals to compare: " + c2.equals(c3));
System.out.println();

int i1 = 10000;
Integer i2 = new Integer(10000);
Integer i3 = new Integer(10000);
System.out.println("Integer primitive types and wrapped objects use == comparison: " + (i1 == i2));
System.out.println("Integer primitive types and wrapped objects are compared using equals: " + i2.equals(i1));
System.out.println("Integer wrapper objects and wrapper objects are compared using ==: " + (i2 == i3));
System.out.println("Integer wrapped objects and wrapped objects are compared using equals: " + i2.equals(i3));
System.out.println();

long l1 = 1000000000000000L;
Long l2 = new Long("1000000000000000");
Long l3 = new Long("1000000000000000");
System.out.println("Long primitive types and wrapped objects use == comparison: " + (l1 == l2));
System.out.println("Long primitive types and wrapped objects are compared using equals: " + l2.equals(l1));
System.out.println("Long wrapper objects and wrapper objects use == comparison: " + (l2 == l3));
System.out.println("Long wrapped objects and wrapped objects are compared using equals: " + l2.equals(l3));
System.out.println();

float f1 = 10000.111F;
Float f2 = new Float("10000.111");
Float f3 = new Float("10000.111");
System.out.println("Float primitive types and wrapped objects use == comparison: " + (f1 == f2));
System.out.println("Float primitive types and wrapped objects are compared using equals: " + f2.equals(f1));
System.out.println("Float wrapper objects and wrapper objects are compared using ==: " + (f2 == f3));
System.out.println("Float wrapper object and wrapper object use equals to compare: " + f2.equals(f3));
System.out.println();

double d1 = 10000.111;
Double d2 = new Double("10000.111");
Double d3 = new Double("10000.111");
System.out.println("Double primitive types and wrapped objects use == comparison: " + (d1 == d2));
System.out.println("Double primitive types and wrapped objects are compared using equals: " + d2.equals(d1));
System.out.println("Double wrapper objects and wrapper objects use == comparison: " + (d2 == d3));
System.out.println("Double wrapper object and wrapper object are compared using equals: " + d2.equals(d3));
System.out.println();

boolean bl1 = true;
Boolean bl2 = new Boolean("true");
Boolean bl3 = new Boolean("true");
System.out.println("Boolean primitive types and wrapped objects use == comparison: " + (bl1 == bl2));
System.out.println("Boolean primitive types and wrapped objects are compared using equals: " + bl2.equals(bl1));
System.out.println("Boolean wrapper objects and wrapper objects are compared using ==: " + (bl2 == bl3));
System.out.println("Boolean wrapper objects and wrapper objects use equals for comparison: " + bl2.equals(bl3));

operation result


Byte primitive types and wrapper objects use == comparison: true
Byte primitive types and wrapper objects use equals for comparison: true
Byte wrapper objects and wrapper objects use == comparison: false
Byte wrapper objects and wrapper objects use equals for comparison: true

Short primitive types and wrapper objects use == comparison: true
Short primitive types and wrapper objects use equals for comparison: true
Short wrapper objects and wrapper objects use == comparison: false
Short wrapper objects and wrapper objects use equals for comparison: true

Character primitive types and wrapper objects use == comparison: true
Character primitive types and wrapper objects are compared using equals : true
Character wrapper objects and wrapper objects use == comparison: false
Character wrapper objects and wrapper objects use equals for comparison: true

Integer primitive types and wrapper objects use == comparison: true
Integer primitive types and wrapper objects use equals for comparison: true
Integer wrapper objects and wrapper objects use == comparison: false
Integer wrapper objects and wrapper objects use equals for comparison: true

Long primitive types and wrapper objects use == comparison: true
Long primitive types and wrapper objects use equals for comparison: true
Long wrappers and wrapped objects are compared using == : false
Long wrapper objects and wrapper objects use equals for comparison: true

Float primitive types and wrapper objects use == comparison: true
Float primitive types and wrapper objects use equals for comparison: true
Float wrapper objects and wrapper objects use == comparison: false
Float wrapper objects and wrapper objects use equals for comparison: true

Double primitive types and wrapped objects use == comparison: true
Double primitive types and wrapper objects use equals for comparison: true
Double wrapped objects and wrapped objects are compared using == : false
Double wrapped objects and wrapped objects use equals for comparison: true

Boolean primitive types and wrapper objects are compared using == : true
Boolean primitive types and wrapper objects are compared using equals : true
Boolean wrapper objects and wrapper objects are compared using == : false
Boolean wrapper objects and wrapper objects use equals for comparison: true