• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

declaration and Initialization

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic