Skip to content
Advertisement

How do I use the character’s equals() method in Java?

As the String is an object, you need to use the string.equals(string) method to confirm that the two strings are equal.

However, why do you use the == to check if two chars are equal, char1 == char2, rather than char1.equals(char2)?

Advertisement

Answer

Perhaps, this will help to understand the difference on == vs equals.

  @Test
  public void testCharacterEquals() {
    //primitive type uses == operator for equals comparasion
    char a1 = 'A';
    char a2 = 'A';
    if (a1 == a2) {
      System.out.println("primitive type comparasion: it's equal");
    }

    //From Java doc; The Character class wraps a value of the primitive type char in an object. An object of type Character contains a single field whose type is char.
    //Object type uses equals method for equals comparasion
    Character character1 = 'A';
    Character character2 = 'A';
    if (character1.equals(character2)) {
      System.out.println("object type comparasion: it's equal");
    }
  }
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement