How to Compare Strings in Java Correctly?

How to Compare Strings in Java Correctly?, I know many of you are using == to compares two strings in Java.Well, the == is correct.In ==, reference equality will be matched.Means that, it is checking that, they are same objects or not.
We have to use the .equals() for actually matching the values. .equals(), value equality will be matched.Means that,it is checking that, they are logically equal or not.
To Compare Strings in Java Correctly, we can use either of them, as per the situations.
ALso,if you want to check whether two strings have the same value, Objects.equals() is the one you are looking for.

// Check they have same value
new String("string_test").equals("string_test") 
// Will give you: --> true 

//   In this case, they are not same object
new String("string_test") == "string_test" 
// Will give you: --> false 

new String("string_test") == new String("string_test") 
// Will give you: --> false 


//Refer to the same object will give you true
"test" == "test" 
// Will give you: --> true 

// Test for nulls and calls .equals()
Objects.equals("string_test", new String("string_test")) // Result is true
Objects.equals(null, "string_test") // Result is false

That comes to an conclusion.You can,almost and always want to use Objects.equals().And when you are dealing with interned strings you can use “==”.
Hope,that helps.