This example shows how to compare two string values in Java. Different ways to check if two strings are equal or not using Object.equals() method.
— Example shows different ways of comparing two String Values in Java.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
new String("test").equals("test") // --> true new String("test") == "test" // --> false new String("test") == new String("test") // --> false "test" == "test" // --> true "test" == "te" + "st" // --> true Objects.equals("test", new String("test")) // --> true Objects.equals(null, "test") // --> false Objects.equals(null, null) // --> true |