Saturday, July 3, 2021

Java String Comparison

In Java, the == operator compares references i.e. if two reference variables point to the same object in the heap, it returns true, otherwise it is false.

The correct way to compare strings in Java is to either use:

equals(), equalsIgnoreCase(), or compareTo()

 Behavior of == and equals() varies in case of String but remain the same in case of Object(), because String class overrides equals() method from Object class.


No comments:

Post a Comment