Introduction
There are two ways to compare strings in Java
- "==" operator
[Compares references not
values]
- ".equals()" method, derived from object class, i.e. java.lang.object.equals()
[Compares values for
equality]
Note: .equals() method is automatically
defined for each and every class, because it is defined in Object class, from
which all other classes are derived.
Syntax to define an object of String type:
String str = "This is a string object."
where str is an object of String type.
== versus .equals()
Code:
Join the conversation! Your thoughts help the community grow.