0

Why is second SOP showing output as true here, I was hoping it would display false like first SOP ?

public class reflect1 {
 public static void main(String[] args) {
 Reflect1A obj1 = new Reflect1A();
 Reflect1A obj2 = new Reflect1A();
 System.out.println(obj1 == obj2);
 Class c1 = obj1.getClass();
 Class c2 = obj2.getClass();
 System.out.println(c1 == c2);
 } 
 }
class Reflect1A {
}
techGaurdian
7481 gold badge15 silver badges35 bronze badges
asked Dec 1, 2014 at 16:23
1
  • The instance of the class String is the same for all the instances of the String object. Commented Dec 1, 2014 at 16:32

6 Answers 6

3

From the Java Language Specification

  • The method getClass returns the Class object that represents the class of the object.

A Class object exists for each reference type. It can be used, for example, to discover the fully qualified name of a class, its members, its immediate superclass, and any interfaces that it implements.

Since both your objects are of type Reflect1A, they both return the same Class object.

You would get the same object by doing

Class<?> clazz = Class.forName("com.example.Reflect1A")
System.out.println(c1 == clazz); // true

(though this is not necessarily required by all classloaders.)

answered Dec 1, 2014 at 16:27
Sign up to request clarification or add additional context in comments.

Comments

3

The values of obj1 and obj2 refer to different objects - when you use == in Java and both operands are references, the result is to compare whether those references refer to the exact same object. In this case you've got two different objects, so the references are not the same.

However, they're both of the same class, so that's why c1 == c2 is true.

answered Dec 1, 2014 at 16:27

Comments

1

The first line prints false because it is a different instance of the same class.

The second line prints true because it is the same class type. There is a obscure gotcha here to be aware of, if you're in a multiple classloader environment, e.g. an application server like JBoss, or OSGI etc, it is possible for two class instances to not be equal

answered Dec 1, 2014 at 16:27

Comments

1

An object is equal (==) only to itself. So clearly both getClass() statements are returning the same Class object

answered Dec 1, 2014 at 16:27

Comments

0

Because obj1 and obj2 are different instances (first check is false) of the same type (class - second check is true).

https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#getClass%28%29

why did you expect to return false?

answered Dec 1, 2014 at 16:27

Comments

0

The operator == compares the references in which the objects are created by default

Obj1 and Obj2 are of same type class Reflect1. For these objects are equal only when compared like this obj1.equal(obj2).

While the class type of obj1 and obj2 are the same == operation will be true.

answered Dec 1, 2014 at 17:16

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.