Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between references and objects in java? [duplicate]

I have class GUI so I can create object like this:

GUI g1 = new GUI();

and a reference variable like this:

GUI g2;

Now as much as I know, g2 is a reference variable which gives reference to GUI class and g1 is an object of GUI class. What is the difference between g1 and g2? I can use property of GUI class with object but what is the possible usage of g2?

like image 217
Rahul Virpara Avatar asked Feb 12 '12 09:02

Rahul Virpara


People also ask

What is difference between object and object reference?

Student obj = new Student(); The objects are created in the heap area and, the reference obj just points out to the object of the Student class in the heap, i.e. it just holds the memory address of the object (in the heap).

Is object and reference variable same?

A reference variable is a variable that points to an object of a given class, letting you access the value of an object. An object is a compound data structure that holds values that you can manipulate. A reference variable does not store its own values.

What is the relationship between a reference and an object?

Reference is a relationship between objects in which one object designates, or acts as a means by which to connect to or link to, another object. The first object in this relation is said to refer to the second object. It is called a name for the second object.

What is the difference between object instance and reference?

Reference points to the Object. Instance is the copy of the Reference that points to object at a point of time. Refrence is a variable that points the objects. Object is the instance of the class that have some memory and instance is a variable & methods that object have.


2 Answers

References are names. Objects are stuff. You can have different names for stuff, even for stuff that doesn't actually exist.

You can declare names, without actually giving them any "real" meaning, like this:

GUI g1;

You can assign meaning (real stuff to refer to) to names with the = operator:

GUI g1 = some_gui;

Names can change their meaning over time. The same name can refer to different things at different points in history.

GUI g1 = some_gui;

doSomething();

g1 = some_other_gui;

There are also synonyms: multiple names can refer to the same thing:

GUI g2 = g1;

That's pretty much what references do. They are names meant to refer to stuff.

Stuff can be created:

new GUI();

Stuff can be created and named on the spot for later reference (literally!):

GUI g1 = new GUI();

And stuff can be referred to, using its name (or any of its names!):

g1.doSomething();
g2.doSomethingAgain();

Different stuff of the same kind (Class) can be created, and named differently:

GUI g1 = new GUI();
GUI g2 = new GUI();
GUI g3 = new GUI();

GUI g1_synonym = g1;

:)

like image 188
slezica Avatar answered Oct 18 '22 01:10

slezica


What is difference between references and objects in java?

A reference is an entity which provides a way to access object of its type. An object is an entity which provides a way to access the members of it's class or type.

Generally, You can't access an object without a reference to it.

class GUI
{
    void aMethod()
    {
        // some business logic.
    }
}

You can call aMethod with or without a reference. but you definitely need an object.

Without Reference:

new GUI().aMethod(); 
// you can't reuse the object
// bad way to code.

With Reference:

GUI aGUIReference = new GUI();
aGUIReference.aMethod();
// Now, the object can be reused.
// Preferred way to code

Now a little explanation to your code lines:

GUI g1 = new GUI();
// g1 is a reference to an object of GUI class.

GUI g2;
// g2 is a reference that can point to an object of GUI class
// but currently not pointing to any.

The only difference b/w g1 and g2 is that g1 is initialized with an object but g2 points to null

g2 = g1;
// it means g2 will point to the same object g1 is pointing to
// only one object but two references.
like image 23
Azodious Avatar answered Oct 18 '22 01:10

Azodious