Why do we first declare subtypes as their supertype before we instantiate them?

前端 未结 12 1150
轻奢々
轻奢々 2021-02-01 19:27

Reading other people\'s code, I\'ve seen a lot of:

List ints = new ArrayList();
Map map = new HashMap();
12条回答
  •  轮回少年
    2021-02-01 20:01

    It is just easier to think of String as of String. As well as it's easier (and more beneficial) to think of WhateverList as of List.

    The bonuses are discussed many times, but in brief you simply separate the concerns: when you need a CharSequence, you use it. It's highly unlikely that you need ArrayList only: usually, any List will do.

提交回复
热议问题