Wang SL
Wang SL

Reputation: 29

Does java subclass use the same superclass field object

I am currently learning Java. Look at the code below:

package classtest1;

class ClassSuper
{
    public Object myObject = new Object();

    public ClassSuper(){}
}

public class ClassTest1 extends ClassSuper
{
    public ClassTest1()
    {
        System.out.println("this.myObject.equals(super.myObject) return: " + this.myObject.equals(super.myObject));
        System.out.println("this.myObject == (super.myObject) return: " + this.myObject == (super.myObject));
    }

    public static void main(String[] args)
    {
        ClassTest1 myClassTest1 = new ClassTest1();
    }

}

the output is below:

run:
this.myObject.equals(super.myObject) return: true
false
BUILD SUCCESSFUL (total time: 0 seconds)

My question is that, why equals and "==" are not the same? Why output false when using "==". Will Subclass create a new copy myObject in memory?

Upvotes: 2

Views: 90

Answers (3)

09Q71AO534
09Q71AO534

Reputation: 4440

You are confused with the output produced so below are few points you need to focus on.

Operator Preferences : Java has well-defined rules for specifying the order in which the operators in an expression are evaluated when the expression has several operators.

For your better understanding check the try executing the below code.

public class ClassTest1 extends ClassSuper
{
    public ClassTest1()
    {
        System.out.println("this.myObject.equals(super.myObject) return: " + (this.myObject.equals(super.myObject)));
        System.out.println("this.myObject == (super.myObject) return: " + this.myObject == (super.myObject));
        System.out.println("this.myObject == (super.myObject) return: " + (this.myObject == (super.myObject)));
    }

   ...

}

Console of your program with above changes.

this.myObject.equals(super.myObject) return: true
false
this.myObject == (super.myObject) return: true

References:

Upvotes: 0

Mohammed Imran
Mohammed Imran

Reputation: 51

System.out.println("this.myObject == (super.myObject) return: " + this.myObject == (super.myObject));
Actually this is comparing strings. First String is
"this.myObject == (super.myObject) return: " + this.myObject
Second String is
(super.myObject)
If you want to compare objects use small bracket as
(this.obj == (super.obj))

Upvotes: 0

Eran
Eran

Reputation: 393801

Will Subclass create a new copy myObject in memory?

No. You are simply not comparing the Objects you think you are comparing.

System.out.println("this.myObject == (super.myObject) return: " + this.myObject == (super.myObject));

compares the String "this.myObject == (super.myObject) return: " + this.myObject to (super.myObject) and returns false.

When the argument passed to System.out.println is evaluated, it is evaluated from left to right. First this.myObject.toString() is concatenated to "this.myObject == (super.myObject) return: ", and then the resulting String is compared to (super.myObject) with the == operator.

If you wrap the comparison with parentheses :

System.out.println("this.myObject == (super.myObject) return: " + (this.myObject == super.myObject));

you'll get the comparison you intended which will return true, since this.myObject and super.myObject refer to the same Object.

Upvotes: 7

Related Questions