CODE
class EqualsDemo
{
public static void main(String arg[])
{
String one = "Hyderabad";
String two = "HYDERABAD";
String three = "Hyderabad";
String four = "Kakinada";
System.out.println(one + " equals " + three + " -- " + one.equals(three));
System.out.println(one + " equals " + two + " -- " + one.equals(two));
System.out.println(one + " equals IgnoreCase " + two + " -- "
+ one.equalsIgnoreCase(two));
System.out.println(one + " equals " + four + " -- " + one.equals(four));
}
}
Hyderabad equals Hyderabad -- true
Hyderabad equals HYDERABAD -- false
Hyderabad equals IgnoreCase HYDERABAD -- true
Hyderabad equals Kakinada -- false
The program consists of four output lines.
one
is compared and matched with each character of three
using equals()
. Since both contain "Hyderabad"
it returns true
one
doesnot match with characters of two(HYDERABAD)
due to case difference.one(Hyderabad)
matches with characters of two(HYDERABAD)
even if there is case difference since we are using equalsIgnoreCase()
.one(Hyderabad)
does not match with each character of four(Kakinada)
using equals()
.equals
and equalsIgnoreCase
.