“Object” vs “Object Variable” in Java?

后端 未结 6 1567
春和景丽
春和景丽 2021-02-01 19:01

I am teaching myself Java, and one of the review exercises in my book asks for the difference between an \"object\" and an \"object variable.\"

I know what an object is

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-01 19:48

    I'll bite.

    The Object is the instance itself, whereas the Object Variable is the reference to the Object.

    Here's a contrived example:

    Object o = new Object();
    Object ref1 = o;
    

    In his case, there is a single instance of the Object, but it is referenced by two Object Variables: o and ref1.

    When an Object is no longer referenced by an Object Variable, the Object is garbage collected.

提交回复
热议问题