aspose file tools*
The moose likes Beginning Java and the fly likes Difference between Object and Reference, in this Scenario. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between Object and Reference, in this Scenario. " Watch "Difference between Object and Reference, in this Scenario. " New topic
Author

Difference between Object and Reference, in this Scenario.

N.Senthil Kumar
Ranch Hand

Joined: Jul 07, 2009
Posts: 50


May i know the Difference between these three expressions and where and which to use.


HashMap map = null;
map = map2;

and

HashMap map = map2;

and

HashMap map = new HashMap();
map = map2;


Thanks




N.Senthil Kumar
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

The first and third versions make an unnecessary assignment to the variable; the third is really terrible because you create an object that is never used for anything. The second version does only as much work as is needed, and no more.

Now, I don't know what that has to do with your subject? An object is like a Dog, and a reference is like a Leash. It connects you to the Dog; it provides a way to refer to the Dog. A variable is a reference.


[Jess in Action][AskingGoodQuestions]
N.Senthil Kumar
Ranch Hand

Joined: Jul 07, 2009
Posts: 50
Ernest Friedman-Hill wrote:The first and third versions make an unnecessary assignment to the variable; the third is really terrible because you create an object that is never used for anything. The second version does only as much work as is needed, and no more.


Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between Object and Reference, in this Scenario.