equals(…) and equalsIgnoreCase(…)

后端 未结 7 880
遇见更好的自我
遇见更好的自我 2021-01-01 19:44

Why do we have equals() and equalsIgnoreCase() as two different methods, when equals() could have been overloaded with a special

7条回答
  •  囚心锁ツ
    2021-01-01 19:55

    Because equals() method is inherited from Object.

    If they did it as you suggest then we would have something like this:

    public final class String {
    
        public boolean equals () { ... }
    
        public boolean equals (boolean ignoreCase) { ... }
    
    } 
    

    And without reading documentation it would be impossible to understand what method equals() (which without parameter) do.

提交回复
热议问题