简体   繁体   中英

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?

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 .

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))

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM