Is Java's assertEquals method reliable?

前端 未结 7 1741
[愿得一人]
[愿得一人] 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条回答
  •  旧时难觅i
    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/

提交回复
热议问题