Is Java's assertEquals method reliable?

前端 未结 7 1690
[愿得一人]
[愿得一人] 2020-11-29 18:05

I know that == has some issues when comparing two Strings. It seems that String.equals() is a better approach. Well, I\'m doing JUni

相关标签:
7条回答
  • 2020-11-29 18:22

    assertEquals uses the equals method for comparison. There is a different assert, assertSame, which uses the == operator.

    To understand why == shouldn't be used with strings you need to understand what == does: it does an identity check. That is, a == b checks to see if a and b refer to the same object. It is built into the language, and its behavior cannot be changed by different classes. The equals method, on the other hand, can be overridden by classes. While its default behavior (in the Object class) is to do an identity check using the == operator, many classes, including String, override it to instead do an "equivalence" check. In the case of String, instead of checking if a and b refer to the same object, a.equals(b) checks to see if the objects they refer to are both strings that contain exactly the same characters.

    Analogy time: imagine that each String object is a piece of paper with something written on it. Let's say I have two pieces of paper with "Foo" written on them, and another with "Bar" written on it. If I take the first two pieces of paper and use == to compare them it will return false because it's essentially asking "are these the same piece of paper?". It doesn't need to even look at what's written on the paper. The fact that I'm giving it two pieces of paper (rather than the same one twice) means it will return false. If I use equals, however, the equals method will read the two pieces of paper and see that they say the same thing ("Foo"), and so it'll return true.

    The bit that gets confusing with Strings is that the Java has a concept of "interning" Strings, and this is (effectively) automatically performed on any string literals in your code. This means that if you have two equivalent string literals in your code (even if they're in different classes) they'll actually both refer to the same String object. This makes the == operator return true more often than one might expect.

    0 讨论(0)
  • 2020-11-29 18:32

    "The == operator checks to see if two Objects are exactly the same Object."

    http://leepoint.net/notes-java/data/strings/12stringcomparison.html

    String is an Object in java, so it falls into that category of comparison rules.

    0 讨论(0)
  • 2020-11-29 18:33
    public class StringEqualityTest extends TestCase {
        public void testEquality() throws Exception {
            String a = "abcde";
            String b = new String(a);
            assertTrue(a.equals(b));
            assertFalse(a == b);
            assertEquals(a, b);
        }
    }
    
    0 讨论(0)
  • 2020-11-29 18:35

    Yes, it is used all the time for testing. It is very likely that the testing framework uses .equals() for comparisons such as these.

    Below is a link explaining the "string equality mistake". Essentially, strings in Java are objects, and when you compare object equality, typically they are compared based on memory address, and not by content. Because of this, two strings won't occupy the same address, even if their content is identical, so they won't match correctly, even though they look the same when printed.

    http://blog.enrii.com/2006/03/15/java-string-equality-common-mistake/

    0 讨论(0)
  • 2020-11-29 18:38

    The JUnit assertEquals(obj1, obj2) does indeed call obj1.equals(obj2).

    There's also assertSame(obj1, obj2) which does obj1 == obj2 (i.e., verifies that obj1 and obj2 are referencing the same instance), which is what you're trying to avoid.

    So you're fine.

    0 讨论(0)
  • 2020-11-29 18:40

    You should always use .equals() when comparing Strings in Java.

    JUnit calls the .equals() method to determine equality in the method assertEquals(Object o1, Object o2).

    So, you are definitely safe using assertEquals(string1, string2). (Because Strings are Objects)

    Here is a link to a great Stackoverflow question regarding some of the differences between == and .equals().

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