简体   繁体   中英

Object vs Reference in Java

In Java, if I declare,

MyClass obj;

Is obj called a "reference" or an "object". I am not instantiating class here.

obj is a Reference to an instance of MyClass.

Currently, that Reference is NULL because you haven't assigned it to refer to any instance.

Technically MyClass must be a subclass of Object, so it is possible to say that obj is a Reference to an instance of Object as well.

Reference : A variable that points to some object in memory. It is stored in stack they can be contained in other objects (then they are not really variables, but fields), which puts them on the heap also.

Object : An instance of class that is created dynamically. It is stored in heap

Example:

MyClassI aObj,aObj1;

aObj=new MyClass2();

At first line aObj and aObj1 are references

At second line aObj referencing to object of MyClass2(New operator creates an object of Myclass2 and its address is assigned to aObj).

To understand even better consider a class Car which has driverName as a member.

Car c1,c2;

c1.driverName="Andrew"
c2.driverName="Gabriel"

System.out.println(c1.driverName);//gives Andrew
System.out.println(c2.driverName);//gives Gabriel

c1=c2;

c2=null;

// gives gabriel because the address of c2 is copied to reference c1.
// the object is not nullified because c2 is just a reference when 
// assigning null the address that is stored on c2 in nullified not 
// the object it points..

system.out.println(c1.driverName);

In computer science, a reference is a value that enables a program to indirectly access a particular data item, such as a variable or a record, in the computer's memory or in some other storage device. The reference is said to refer to the data item, and accessing that data is called dereferencing the reference.

In computer science, an object is any entity that can be manipulated by the commands of a programming language, such as a value, variable, function, or data structure. (With the later introduction of object-oriented programming the same word, "object", refers to a particular instance of a class)

so obj is a reference and new MyClass() can be seen as an object

Sometimes you'll hear people say "Design an method that takes an object as a parameter and..."

If you're new to programming, and especially with Java, such statements can lead to some confusion. These people are using the word "object" to refer to an instance of a class in very general OOP terms, not necessarily Java specific.

When we're talking specifics about Java and the code you have there, it is a reference to an instance of MyClass , which is NULL.

  • In Java, all objects are accessed by reference, and you never have direct access to the object itself.
  • reference :- is a variable that has a name and can be used to access the contents of an object, A reference can be assigned to another reference passed to a method, or returned from a method. All references are the same size, no matter what their type is Like "Object object ;".

  • object:- is an entity that's exists in memory allocated by the Java run time environment, An object sits on the heap and does not have a name Like "Object object=new Object();".

  • so MyClass obj Here is A reference referred to Null.

  • We can summarize this principle with the following two rules:

    1. The type of the object determines which properties exist within the object in memory.
    2. The type of the reference to the object determines which methods and variables are accessible to the Java program.

obj is a Reference of type MyClass. The current reference does not point to anything (ie: null).

'obj' is a variable . It holds either a reference or null. If it holds a reference, that refers to an object.

The reference is a variable that has a name and can be used to access the contents of an object. A reference can be assigned to another reference, passed to a method, or returned from a method.

All references are the same size, no matter what their type is. An object sits on the heap and does not have a name. Therefore, you have no way to access an object except through a reference. Objects come in all different shapes and sizes and consume varying amounts of memory. An object cannot be assigned to another object, nor can an object be passed to a method or returned from a method. It is the object that gets garbage collected, not its reference.

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