Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

declaration and Initialization

 
Prasath Thirumoorthy
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Chetan Raju
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Isuru Sampath
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15203
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic