What's the difference between these two java variable declarations?

后端 未结 5 1716
小蘑菇
小蘑菇 2020-12-21 17:10
public class SomeClass {
    private HashSet contents = new HashSet();
    private Set contents2 = new HashSet&         


        
5条回答
  •  臣服心动
    2020-12-21 17:39

    One thing worth to mention, is that interface vs. concrete class rule is most important for types exposed in API, eg. method parameter or return type. For private fields and variables it only ensures you aren't using any methods from concrete implementation (i.e. HashSet), but then it's private, so doesn't really matter.

    Another thing is that adding another type reference will slightly increase size of your compiled class. Most people won't care, but these things adds up.

提交回复
热议问题