equals(…) and equalsIgnoreCase(…)

后端 未结 7 881
遇见更好的自我
遇见更好的自我 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.

    0 讨论(0)
  • 2021-01-01 20:04

    I think they just chose one of the alternatives. .NET chose the other. StringComparison.InvariantCultureIgnoreCase etc.

    Definitely what you are suggesting and [even better what] .NET implemented would have been more flexible for different cultures etc. In fact I don't even know what culture they use in this ignore case. I guess Current culture.

    0 讨论(0)
  • 2021-01-01 20:05

    equalIgnoreCase() is used for ignore the Case sensitive of our String. But the equals() is only returns true, while be same case of string

    ex,

    String value="java";
    if(value.equals("JAva")
    {
        System.out.println("Return True");
    }
    else
    {
        System.out.println("Return False");
    }
    

    Ans: Returns False

    but the other one is,

    if(value.equalIgnoreCase("JAva")
    {
        System.out.println("Return True");
    }
    else
    {
        System.out.println("Return False");
    }
    

    Ans: Returns True

    0 讨论(0)
  • 2021-01-01 20:08
        // Demonstrate equals() and equalsIgnoreCase(). 
        class equalsDemo { 
        public static void main(String args[]) { 
        String s1 = "Hello"; 
        String s2 = "Hello"; 
        String s3 = "Good-bye"; 
        String s4 = "HELLO"; 
        System.out.println(s1 + " equals " + s2 + " -> " + 
        s1.equals(s2)); 
        System.out.println(s1 + " equals " + s3 + " -> " + 
        s1.equals(s3)); 
        System.out.println(s1 + " equals " + s4 + " -> " + 
        s1.equals(s4)); 
        System.out.println(s1 + " equalsIgnoreCase " + s4 + " -> " + 
        s1.equalsIgnoreCase(s4)); 
        } 
    }
    

    The output from the program is shown here:

    Hello equals Hello -> true 
    Hello equals Good-bye -> false 
    Hello equals HELLO -> false 
    Hello equalsIgnoreCase HELLO -> true
    
    0 讨论(0)
  • 2021-01-01 20:13

    It's absolutely possible to do what you are suggesting but the language designers chose to go the other way and hence we have equalsIgnoreCase(otherString) instead of say equals(otherString, StringConstants.IGNORE_CASE) or equals(otherString, true).

    0 讨论(0)
  • 2021-01-01 20:13

    The main test when overridng a method with additional parameters is that I would expect any method override to do exactly the same thing as the method it's overriding. Equals(), being derived from Object has a contract it must follow. Two objects that are equal() should have identical hashcodes. I don't think two objects that are case insensitive equal should have the same hashcode, so I believe overriding equal here is the wrong thing to do.

    0 讨论(0)
提交回复
热议问题