File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between Object and Reference, in this Scenario.

 
N.Senthil Kumar
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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


 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
N.Senthil Kumar
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic