The difference between Object objects and object reference?

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

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

#16 RozenKristal  Icon User is offline

  • D.I.C Regular

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

Re: The difference between Object objects and object reference?

Posted 25 April 2013 - 04:01 PM

View Postjon.kiparsky, on 25 April 2013 - 04:00 PM, said:

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

What purpose could it serve?


The purpose of it? Prob help me gain a few points on the test!!! LOL, honestly, I myself don't see why I should use it at all...
Was This Post Helpful? 0
  • +
  • -

#17 jjh08  Icon User is offline

  • D.I.C Head

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

Re: The difference between Object objects and object reference?

Posted 25 April 2013 - 04:05 PM

Quote

is that mean a variable contain the object reference of Object class?

Remember what jon.kiparsky said earlier:

Quote

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

In other words, once you declare and assign a reference variable to a specific object, you can get that object's value, primarily through something like hashCode().
Quick example:
public class Object
{
	public static void main(String[] args)
	{
		Object o1 = new Object();
		Object o2 = new Object();
		System.out.println(o1.hashCode());
		System.out.println(o2.hashCode());
	}
}


As for your second question, I don't think I follow your professor :no:
Was This Post Helpful? 0
  • +
  • -

#18 RozenKristal  Icon User is offline

  • D.I.C Regular

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

Re: The difference between Object objects and object reference?

Posted 25 April 2013 - 04:07 PM

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

Quote

is that mean a variable contain the object reference of Object class?

Remember what jon.kiparsky said earlier:

Quote

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

In other words, once you declare and assign a reference variable to a specific object, you can get that object's value, primarily through something like hashCode().
Quick example:
public class Object
{
	public static void main(String[] args)
	{
		Object o1 = new Object();
		Object o2 = new Object();
		System.out.println(o1.hashCode());
		System.out.println(o2.hashCode());
	}
}


As for your second question, I don't think I follow your professor :no:/>



I think he probably saying something like Object is a base class, and people can create instance of other classes, but why not the base class Object, why is it useless to do so?
Was This Post Helpful? 0
  • +
  • -

#19 pbl  Icon User is offline

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

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

Re: The difference between Object objects and object reference?

Posted 25 April 2013 - 04:19 PM

Don't see any case where you would have to do

Object o = new Object();

you can have Object returned by other methods

Object o = objectInputStream.nextObject();
Was This Post Helpful? 2
  • +
  • -

#20 jjh08  Icon User is offline

  • D.I.C Head

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

Re: The difference between Object objects and object reference?

Posted 25 April 2013 - 04:30 PM

Quote

I think he probably saying something like Object is a base class, and people can create instance of other classes, but why not the base class Object, why is it useless to do so?

That does not make sense to me. Every time you create an object, you inherit from class Object. Not only does the subclasses contain the functionality of Object (i.e. clone(), equals(), finalize(), etc), it contains within it a sub-object of the base class (which in our case, would be Object). We can verify this by creating a constructor of our superclass and our subclass.
class Duck
{
	Duck()
	{
		System.out.println("Duck()");
	}
}

class RubberDuck extends Duck
{
	RubberDuck()
	{
		System.out.println("RubberDuck()");
	}
}

public class Demo
{
	public static void main(String[] args)
	{
		new RubberDuck();
	}
}


If you run this, the output will be:
Duck()
RubberDuck()
I only instantiated the subclass RubberDuck but the compiler implicitly made a call via super() to its base class which is Duck and it will print Duck() first because the subclass automatically gets a subobject of the superclass.
In the case of Object, if I'm not mistaken, the compiler is doing the same thing. In other words, in this example, it actually made a call to class Duck's superclass which would be Object. However, the default constructor of Object would be automatically empty so it wouldn't print anything...I guess that's what is happening :P
Was This Post Helpful? 1
  • +
  • -

#21 RozenKristal  Icon User is offline

  • D.I.C Regular

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

Re: The difference between Object objects and object reference?

Posted 25 April 2013 - 04:32 PM

Thank folks, I think I understand 90% of it.
Was This Post Helpful? 0
  • +
  • -

#22 pbl  Icon User is offline

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

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

Re: The difference between Object objects and object reference?

Posted 25 April 2013 - 04:36 PM

jjh08
Yes we all know that when you call a constructor the super class constructor is called up to the Object constructor.

The question is when would you need a construct like

Object o = new Object();
Was This Post Helpful? 0
  • +
  • -

#23 jjh08  Icon User is offline

  • D.I.C Head

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

Re: The difference between Object objects and object reference?

Posted 25 April 2013 - 04:39 PM

View Postpbl, on 25 April 2013 - 04:36 PM, said:

The question is when would you need a construct like

Object o = new Object();

Ok, now I get it. I wasn't understanding the professor's question correctly :P
Was This Post Helpful? 1
  • +
  • -

#24 RozenKristal  Icon User is offline

  • D.I.C Regular

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

Re: The difference between Object objects and object reference?

Posted 25 April 2013 - 04:39 PM

View Postpbl, on 25 April 2013 - 04:36 PM, said:

jjh08
Yes we all know that when you call a constructor the super class constructor is called up to the Object constructor.

The question is when would you need a construct like

Object o = new Object();


Locking? I googled and members on stackOverflow say so.
Was This Post Helpful? 0
  • +
  • -

#25 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 838
  • View blog
  • Posts: 2,575
  • Joined: 29-July 11

Re: The difference between Object objects and object reference?

Posted 25 April 2013 - 04:56 PM

Of course you don't ever HAVE to do O bject o = new Object(), but having the option is nice. Maybe for a synchronized block of code.

Object o = new Object();
synchronized(o) {
    //code here.
}



Of course you don't even have to do that.
Was This Post Helpful? 1
  • +
  • -

#26 RozenKristal  Icon User is offline

  • D.I.C Regular

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

Re: The difference between Object objects and object reference?

Posted 25 April 2013 - 05:05 PM

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

View Postpbl, on 25 April 2013 - 04:36 PM, said:

The question is when would you need a construct like

Object o = new Object();

Ok, now I get it. I wasn't understanding the professor's question correctly :P/>

Aww come on, u are not alone. Even my TA sometimes like, what is going on with the specs....
Was This Post Helpful? 0
  • +
  • -

#27 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7737
  • View blog
  • Posts: 13,068
  • Joined: 19-March 11

Re: The difference between Object objects and object reference?

Posted 25 April 2013 - 06:19 PM

So, yes, you can create a nonce object to synchronize against. But what is that doing? Well, it's basically a unique identifier: the Object instance is a unique object which can be created on the fly, with no special machinery required. So it's useful for that, but any unique identifier would do.

What I was saying about the Object was that in terms of functionality, there's nothing much that you can do with it. It can't store data, or perform actions, or do anything to help your program meet its needs. So unless you needed it for some side effect - like, "it is guaranteed to be the only Object with its particular hash value" - you're not going to create a new Object (except, yes, by inheritance)
Was This Post Helpful? 0
  • +
  • -

#28 pbl  Icon User is offline

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

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

Re: The difference between Object objects and object reference?

Posted 25 April 2013 - 07:10 PM

View Postfarrell2k, on 25 April 2013 - 07:56 PM, said:

Of course you don't ever HAVE to do O bject o = new Object(), but having the option is nice. Maybe for a synchronized block of code.

Object o = new Object();
synchronized(o) {
    //code here.
}



Of course you don't even have to do that.

lloolll)))) why not
Just add a comment in the code explaining what you are doing :)
Was This Post Helpful? 0
  • +
  • -

#29 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 416
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: The difference between Object objects and object reference?

Posted 25 April 2013 - 07:16 PM

I think the main point and Ockham's razor of the topic is that an object is an object, while an object reference is a reference/pointer.

I skimmed over so I don't know if it's been mentioned but an object can have many references and a reference can only point to one object.

Object o1 = new object(); //created one object
Object o2 = o1; //assigned 2 references



Changing a value of o2 will be reflected in o1.

Here is a nice tutoracle on the subject.
Was This Post Helpful? 0
  • +
  • -

#30 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 838
  • View blog
  • Posts: 2,575
  • Joined: 29-July 11

Re: The difference between Object objects and object reference?

Posted 25 April 2013 - 07:23 PM

I can't believe this topic is still going. :lol:
Was This Post Helpful? 1
  • +
  • -

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