File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes declaration and Initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "declaration and Initialization" Watch "declaration and Initialization" New topic
Author

declaration and Initialization

Prasath Thirumoorthy
Ranch Hand

Joined: Jul 23, 2004
Posts: 65
Hi Friends,

please provide me some clarifications:

1. Map mobj = new HashMap();
2. HashMap mobj = new HashMap();

What is the difference between the above two Initializations ?
Which is best way of programming ?

Cheers,
Prasath Thirumoorthy


Thanks,
Prasath

SCJP1.4, SCWCD
Chetan Raju
Ranch Hand

Joined: Aug 02, 2006
Posts: 109
Hello,

Both are valid. In the first, you are using the Interface reference type and in the second you are using the specific class's reference type.

Collections are interface based. By using Interface reference type, you are not making it specific to a collection class. You can simply assign any other collection class object that implements the interface and your code still works.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Declaring it as just a Map means that only methods common to all Map object can be used. That means that, in the future, it would be really easy to change to a different type of Map.

Declaring it as a HashMap means that methods specific to a HashMap could be used. That makes it harder to switch to a different type of Map, which is bad.

However, if the programmer has carefully analysed the situation and decided that HashMap is the only type of Map that is appropriate in the situation, then declaring it as a HashMap is appropriate.

Also, there may be some cases where slight performance benefits will result from knowing the exact type of an object, at compile time.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Isuru Sampath
Ranch Hand

Joined: Jun 26, 2003
Posts: 57


Would be the best choice since you get all the functionalities of Map along with the HashMap. There is no obvious point in creating a HashMap and assigning it to a Map handle since you know the object type when you're writing the code. No need to use Map unless you have no other choice.


No Winds No Waves
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Originally posted by Isuru Sampath:


Would be the best choice since you get all the functionalities of Map along with the HashMap. There is no obvious point in creating a HashMap and assigning it to a Map handle since you know the object type when you're writing the code. No need to use Map unless you have no other choice.

No, that is normally not the best choice. You should program to an interface, not to an implementation. If you use:

Map map = new HashMap();

then it is very easy to replace the HashMap by for example a TreeMap, or another implementation of the Map interface, if that's necessary later on - without the need to change the rest of the application.

The rest of the application doesn't normally need to know exactly which implementation of Map is used.

This allows you to have a less tight coupling between parts in the application. Loose coupling is better than tight coupling.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Agreed. The poster who said that declaring using the implementation type (HashMap) is usually best, is going against the opinions of most programmers I know.

How about the following?

If you are declaring a public API, to be used by all sorts of different classes, you should almost always declare using the interface type (Map).

If you are declaring a local variable or a private field, and not making it visible via a public API, it may be worth declaring using the implementation type (HashMap). It might run a tiny bit faster. You might get the use of one or two additional useful type-specific methods.

If in doubt, use the interface type (Map).
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: declaration and Initialization
 
Similar Threads
HashMap and Map
NaturalLanguageMultiplier strange initialization
Passed by reference or by value?
Sort a Map on the <Key>.
final keyword