Menu
Topics Index
...
`

StringComparisonequals and equalsIgnoreCase Demo - Java Example Program


StringComparisonequals and equalsIgnoreCase Demo
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));    
    }
}
OUTPUT

Hyderabad equals Hyderabad -- true
Hyderabad equals HYDERABAD -- false
Hyderabad equals IgnoreCase HYDERABAD -- true
Hyderabad equals Kakinada -- false

DESCRIPTION

The program consists of four output lines.

  • The first output line is true since each character of the invoking string one is compared and matched with each character of three using equals(). Since both contain "Hyderabad" it returns true
  • The second output line is false since one doesnot match with characters of two(HYDERABAD) due to case difference.
  • The third outputline is true since one(Hyderabad) matches with characters of two(HYDERABAD) even if there is case difference since we are using equalsIgnoreCase().
  • The fourth output is false since the invoking string's each character one(Hyderabad) does not match with each character of four(Kakinada) using equals().

THINGS TO TRY
  • Try with some more example pairs like "window" and "WiNDow". Try both equals and equalsIgnoreCase.

© meritcampus 2019

All Rights Reserved.

Open In App