Parameter. obj - the reference object with which to compare.. Returns. true if these objects are same; false otherwise. Throw. No exception is thrown. Example 1 So the best way to check for equality is using the === operator because it checks value as well as type of both operands. If you want to check for equality between two objects then using String.prototype.valueOf is the correct way. new String ('javascript').valueOf () == new String ('javascript').valueOf () Share. The java.math.BigDecimal.equals() method checks for equality of a BigDecimal value with the object passed. This method considers two BigDecimal objects equal if only if they are equal in value and scale. Syntax: As a general architectural point, I usually advise against letting dependencies on a particular serialization format bleed out beyond your storage/networking layer; thus, I'd first recommend that you consider testing equality between your own application objects rather than their JSON manifestations. Sorted by: 305. 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 String s are Object s) Here is a link to a great Stackoverflow question regarding The statement “0 cannot possibly be represented exactly in a float” is false; 0 is represented with all bits zero (and with the leading bit set to 1 which is distinguished as −0, which equals +0). This does not cause any issue with comparison, and it is not “the main reason == is discouraged.”. – Eric Postpischil. Evgeniy's solution and Michal's reasoning are correct - you don't need to worry about the type of T here. The reason is that the equals method doesn't depend on generics to work correctly. Since Java 7 you can use the static method java.util.Objects.equals(Object, Object) to perform equals checks on two objects without caring about them being null. If both objects are null it will return true , if one is null and another isn't it will return false . Vay Nhanh Fast Money.

how to test equals method in java