-3
package com.泛型.泛型擦除;

public class Main {
    public static void main(String[] args) {
        Object o = new Object();
        Object o1 = new Object();
        System.out.println(o.getClass() == o1.getClass());
    }
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
letme
  • 1
  • 7
    `.getClass()` returns the class of the object, so both `o.getClass()` would return `Object.class`, as would `o1.getClass()`. If you want to compare memory addresses, you would simply use `o` and `o1`: e.g. `System.out.println(o == o1);` – guninvalid Feb 06 '22 at 06:02
  • There is a single Class object per class. – passer-by Feb 06 '22 at 16:32
  • https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#getClass() – Omar Abdel Bari Feb 06 '22 at 16:48

1 Answers1

0

.getClass() gives you the object's class, so comparing the class of two Objects of the same class (Object.class in this case) will always return true.

You can compare the memory adresses (or, more plainly, if the two objects point to the same object in memory) using == - so

System.out.println(new Object() == new Object()); //this will return false, as there's two different objects
Object o1 =  new Object();
Object o2 = o1;
System.out.println(o1==o2); //this will return true, as they're the same object
Itisdud
  • 41
  • 7