How do I use the equality operator in Java?

Equality operator is used to compare two similar things (numbers, characters, boolean, primitives and object references). Equality operator will always result in boolean value (true or false).

For object reference, it will return true if only both reference variables refer to the same object.

package org.kodejava.example.fundamental;

public class EqualityDemo {
    public static void main(String[] args) {
        int value1 = 10, value2 = 10, number1 = 10;
        char a = 'a', b = 'b';
        double number2 = 10d;
        Cat kitty = new Cat("Kitty");
        Cat kitten = new Cat("Kitty");
        Cat sweetie = kitty;

        if (value1 == value2) {
            System.out.println("Equal");
        }

        if (a != b) {
            System.out.println("Not Equal");
        }

        // though it have different type, but it have same value
        if (number1 == number2) {
            System.out.println("Equal");
        }

        // it's not refer to the same object, so it will return
        // false
        if (kitty == kitten) {
            System.out.format("(kitty == kitten) = " + (kitty == kitten));
        } else {
            System.out.println("(kitty == kitten) = " + (kitty == kitten));
        }

        // it's refer to the same object, so it will return true
        if (kitty == sweetie) {
            System.out.println("(kitty == sweetie) = " + (kitty == sweetie));
        }

        if (true != false) {
            System.out.println("true != false");
        }

    }
}

class Cat {
    private String name;

    Cat(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

And here are the result of the program:

Equal
Not Equal
Equal
(kitty == kitten) = false
(kitty == sweetie) = true
true != false

How do I compare if two arrays are equal?

Using Arrays.equals() methods we can compare if two arrays are equal. Two arrays are considered to be equal if their length, each element in both arrays are equal and in the same order to one another.

package org.kodejava.example.util;

import java.util.Arrays;

public class CompareArrayExample {
    public static void main(String[] args) {
        String[] abc = {"Kode", "Java", "Dot", "Org"};
        String[] xyz = {"Kode", "Java", "Dot", "Org"};
        String[] java = {"Java", "Dot", "Com"};

        System.out.println(Arrays.equals(abc, xyz));
        System.out.println(Arrays.equals(abc, java));
    }
}

The Arrays.equals() can be used to compare array of any primitive data type and array of Object. If you run this example you will have a result as follow:

true
false

How do I check string for equality?

To compare string for their content equality we must use the String.equals() method. This method ensures that it will compare the content of both string instead of the object reference of the both string. This method returns true if both string in comparison have the same content.

Do not, never, use the == operator to compare string for its content. The == operator check for object reference equality, it returns true only if both objects point to the same reference. This operator returns false if the object doesn’t point to the same references.

package org.kodejava.example.lang;

public class StringEquals {
    public static void main(String[] args) {
        String s1 = "Hello World";
        String s2 = new String("Hello World");

        // This is good!
        if (s1.equals(s2)) {
            System.out.println("1. Both strings are equals.");
        } else {
            System.out.println("1. Both strings are not equals.");
        }

        // This is bad!
        if (s1 == s2) {
            System.out.println("2. Both strings are equals.");
        } else {
            System.out.println("2. Both strings are not equals.");
        }
    }
}

In the example above we deliberately create an instance of s2 string using the new operator to make sure that we have a different object reference. When you run the program it will give you the following result:

1. Both strings are equals.
2. Both strings are not equals.