The difference between Object objects and object reference?

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

50 Replies - 2810 Views - Last Post: 29 April 2013 - 07:04 PM Rate Topic: ***-- 2 Votes

#1 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 329
  • Joined: 29-September 12

The difference between Object objects and object reference?

Posted 25 April 2013 - 08:35 AM

I'm pretty confused about these 2, I do know that:
Student s = new Student();

s is an object reference to Student.
But what about Object obj?
Will I see things like Object o = new Object() and what category does it fall into, reference or an object???
Is This A Good Question/Topic? 1
  • +

Replies To: The difference between Object objects and object reference?

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,532
  • Joined: 29-July 11

Re: The difference between Object objects and object reference?

Posted 25 April 2013 - 08:38 AM

Both. The o is the reference that points to the Object on the heap. Yes, you may see Object o = new Object in code. You may see it a lot in synchronized blocks of code.

This post has been edited by farrell2k: 25 April 2013 - 08:39 AM

Was This Post Helpful? 1
  • +
  • -

#3 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 329
  • Joined: 29-September 12

Re: The difference between Object objects and object reference?

Posted 25 April 2013 - 08:53 AM

Why is it that for
Object o = new Object();
that it is a reference and an object at the same time? Why for
Student s = new Student();
it is just a reference, why it is not an object of Student ?
I did check google and found that [code]Object o = new Object();[\code] can be used for thread synchro and locking, what other functions it could be used with?

This post has been edited by RozenKristal: 25 April 2013 - 09:02 AM

Was This Post Helpful? 0
  • +
  • -

#4 jjh08  Icon User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 193
  • Joined: 13-July 12

Re: The difference between Object objects and object reference?

Posted 25 April 2013 - 10:03 AM

The new operator returns an object reference. Source.
For Object o = new Object(); 'o' is a reference variable (think of it like a name) for the object reference 'Object' which is on the heap.
Likewise, 's' is an object reference for the 'Student' object.
Quick example:
public class Student
{
	public static void main(String[] args)
	{
		Student s1 = new Student();
		Student s2 = new Student();
		new Student();
	}
}


's1' is a reference variable that refers to the object reference for 'Student'
's2' is another reference variable that refers to the object reference for another 'Student'
'new Student()' instantiates an object 'Student' and returns that object reference.

Does this make sense?

This post has been edited by jjh08: 25 April 2013 - 10:17 AM

Was This Post Helpful? 1
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,849
  • Joined: 19-March 11

Re: The difference between Object objects and object reference?

Posted 25 April 2013 - 12:06 PM

View Postjjh08, on 25 April 2013 - 12:03 PM, said:

Does this make sense?



Yes, it does - this explanation is quite correct.


One clarification:

Quote

'new Student()' instantiates an object 'Student' and returns that object reference.


Since the returned reference is not assigned to some variable, it's lost. You return a reference to a Student object, and then you drop it on the floor and forget about it. Since that object has no live references, it will be collected by the Grim Collector when next it passes through the area.

But the object has really been created: all of the code in its constructor has been executed. (for example, if you update a static variable of the Student class in the constructor, you'll see that the update has happened)



It might help to think of it this way.
Student jim = new Student("James");



This creates a local variable, 'jim'. 'jim' points to a location on the stack.
It also creates an object on the heap, which is an instance of Student and has been initialized with the String "James" (but we don't know what it's done with that String...) The address of this object is the value of the local variable 'jim'.

We can't ever handle the object directly, we have to access it through references. An object reference is really just any sort of handle that we can use to get hold of the object itself. It can be a name, like jim, or a slot in an array or in some Collection.


Don't get too hung up on the case of
Object o = new Object()


o is just another reference to an object on the heap. new Object() is just another constructor that just returns another object.

This post has been edited by jon.kiparsky: 25 April 2013 - 12:07 PM

Was This Post Helpful? 2
  • +
  • -

#6 jjh08  Icon User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 193
  • Joined: 13-July 12

Re: The difference between Object objects and object reference?

Posted 25 April 2013 - 01:52 PM

@jon.kiparsky, Great clarification. One follow up question I would like to ask:

Quote

The address of this object is the value of the local variable 'jim'.

Is it safe to say that an object without a local variable such as 'jim' has no address? I know the object is instantiated via the 'new' keyword but is it just floating around on a lost island waiting for the garbage collector.
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,849
  • Joined: 19-March 11

Re: The difference between Object objects and object reference?

Posted 25 April 2013 - 02:00 PM

It has an address, but nobody knows the address, and there's no way to get it. :)
Hm. Well, I suppose someone might know the address - for example, the garbage collector might operate by keeping track of every object address that's been allocated, and counting the number of references that point to it, and deallocating the memory it occupies when that reference count reaches zero (or it might not). The important thing is, your program has no way of getting hold of that address once you lose it. So it's effectively lost to the world at that point.

Addresses, in Java, don't mean much. You can't do anything with them - you can't address memory, you can't compare addresses, they're not important. References are the key: if you have a reference, you can do things with the object.
Was This Post Helpful? 2
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: The difference between Object objects and object reference?

Posted 25 April 2013 - 02:06 PM

View Postjjh08, on 25 April 2013 - 04:52 PM, said:

Is it safe to say that an object without a local variable such as 'jim' has no address? I know the object is instantiated via the 'new' keyword but is it just floating around on a lost island waiting for the garbage collector.


Yes and no... in certain case the Object may be accessible from another source even if the creator can't directly access it

   class Student {
      static ArrayList<Student> allStudents = new ArrayList<Student>();

      String name;
      // constructor
      Studend(String name) {
         this.name = name;
         allStudents.add(this);
      }
    }



now if you are doing:
    new Student("James")


sure that you can't reference it right away, but it is still accessible through the ArrayList so it cannot be garbage collected
Was This Post Helpful? 2
  • +
  • -

#9 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,849
  • Joined: 19-March 11

Re: The difference between Object objects and object reference?

Posted 25 April 2013 - 02:27 PM

Nice. Yes, pbl's point is important: any reference to the object will save it from the Grim Reaper.
Was This Post Helpful? 0
  • +
  • -

#10 jjh08  Icon User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 193
  • Joined: 13-July 12

Re: The difference between Object objects and object reference?

Posted 25 April 2013 - 02:28 PM

Quote

sure that you can't reference it right away, but it is still accessible through the ArrayList so it cannot be garbage collected

Wow, I didn't know that :P
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,849
  • Joined: 19-March 11

Re: The difference between Object objects and object reference?

Posted 25 April 2013 - 03:08 PM

You can learn a lot by reading everything pbl writes.

This post has been edited by jon.kiparsky: 25 April 2013 - 03:08 PM

Was This Post Helpful? 1
  • +
  • -

#12 jjh08  Icon User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 193
  • Joined: 13-July 12

Re: The difference between Object objects and object reference?

Posted 25 April 2013 - 03:17 PM

View Postjon.kiparsky, on 25 April 2013 - 03:08 PM, said:

You can learn a lot by reading everything pbl writes.

Yes, I've noticed that. He's like a human Java tome :P
Was This Post Helpful? 1
  • +
  • -

#13 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 329
  • Joined: 29-September 12

Re: The difference between Object objects and object reference?

Posted 25 April 2013 - 03:49 PM

Well, I have this question because the professor ask this on the study guide for final:
Explain in tech terms the difference between Object object and object references. Thank to you folks, I understood the object references part; but I still not so clear about Object object? So for that, is that mean a variable contain the object reference of Object class?

Also another question is, "why you will never see a statement that read: Object o = new Object(); that is why I am so hung onto this line. I did a quick google and found that that line can be used for thread synchro but no other usages, can anyone clarify on this?
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: The difference between Object objects and object reference?

Posted 25 April 2013 - 03:59 PM

The question would be more pertinent in C++ where there is a difference between the actual Object and its Object reference. These two concepts simply do not exist in Java all Objects ara access by reference, you cannot, as in C++, directly access an Object.

As far as the Object o = new Object(); is concerned

beside using it as a stub in a Collection of Object I don't see a usefull utility of it.
Was This Post Helpful? 0
  • +
  • -

#15 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,849
  • Joined: 19-March 11

Re: The difference between Object objects and object reference?

Posted 25 April 2013 - 04:00 PM

What would you do with an object of the class Object?

What purpose could it serve?
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »