Why is the map new map map=new HashMap () instead of the new map ();? Because the map is an interface, the interface cannot be instantiated! HashMap is the implementation class of the map interface!

Source: Internet
Author: User
Problem:
May I ask the map map=new HashMap (); Why is this, rather than map map=new map ();
Answer:
Map is an interface, and HashMap is an implementation of map. The interface cannot be instantiated.

Map map=new HashMap (); is to instantiate the map as a hashmap. The benefit of this is that the caller does not need to know the map specific implementation, map interface and the specific implementation of the map Java to help you do.


The change example also proves that I understand the inversion of control in one of the core technologies of Spring MVC:

Refer to my previous article

http://blog.csdn.net/ideality_hunter/article/details/51502828

The relevant description in:

=================================================

There is such a loginaction class, when it wants to call the Adminservice's Hello () method, does not introduce the control inversion, needs its own new, introduces the control inversion, does not have the own new, directly calls on the line.

public class Loginaction extends Actionsupport
{
Private Adminservice adminservice = null;
Public String Execute () throws Exception
{
In spring MVC's inversion of control, spring MVC is just a matter of mentioning us new. So what is the purpose. The separation of interfaces and implementations makes it possible to invoke interfaces without paying attention to how they are implemented.

No control inversion is introduced
Begin
Adminservice aa=new adminserviceimp ();
String B=aa.hello ();
System.out.println ("######################### #BBBBBBBBBBBB" +b);
End

Introducing Control Inversion
Adminservice.hello ();//The code equivalent between the way and//start/End
}

=================================================

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.