Visit: https://edurelation.viden.io
Compare Strings in Java
Compare Strings in JavaTo compare the String objects s1 and s2, use the s1.equals(s2) method.A String comparison with == is incorrect, as == checks for object reference equality. == sometimes gives the correct result, as Java uses a String pool. The following example would work with ==.This would work as expected.String a = "Hello"; String b = "Hello"; if (a==b) { // if statement is true // because String pool is used and // a and b point to the same constant } This comparison would fail.String a = "Hello"; String b = new String("Hello"); if (a==b) { } else { // if statement is false // because String pool is used and // a and b point to the same constant }