Welcome to TechQura.com

Helping users, developers and technologists to write and find solutions
Techqura.com > Coding > JAVA > Q. How to compare strings in Java ?
Techqura.com

How to compare strings in Java ?

Asked by : Tom Warren on 01/03/2021
Response by CodeGuy



Sometimes JAVA if == compares values, because it does some behind-the-scenes stuff but .equals() method always compares a value of String :

String a="Test";
String b="Test";
if(a.equals(b)) ===> true

String a="test";
String b=new String("test");
if(a.equals(b)) ===> true

So using .equals() method is recommended.

The == operator checks to see if the two strings are exactly the same object.

The .equals() method will check if the two strings have the same value.