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.basic;

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 use the && operator in Java?

The && operator also known as conditional-AND operator or short circuit AND. This operator exhibit “short-circuiting” behavior, which means that the second operand is evaluated only if needed.

The && operator evaluate only boolean values. For an AND (&&) expression to be true, both operands must be true. If the first operand resolves false, then the && operator will not evaluate the second operand, because it already know the complete expression will return false.

package org.kodejava.basic;

public class ConditionalANDDemo {
    public static void main(String[] args) {
        // second operand (2<3) is not evaluated, because the first
        // operand return false the result of complete expression
        // can't be true
        boolean a = (5 < 3) && (2 < 3);

        // first operand return true, second operand is evaluated
        // to check the result of the second expression if second
        // operand resolves to true, the complete expression return
        // false, otherwise return false
        boolean b = (1 == 1) && (2 < 3);
        boolean c = (1 == 1) && (5 < 3);

        System.out.println("result a: " + a);
        System.out.println("result b: " + b);
        System.out.println("result c: " + c);
    }
}

The result of the code snippet:

result a: false
result b: true
result c: false

How do I use the shift right “>>” operator?

The signed shift right operator >> shifts a bit pattern to the right. This operator operates with two operand, the left-hand operand to be shifted and the right-hand operand tells how much position to shift.

Shifting a 1000 bit pattern using the >> operator 2 position will produce a 0010 bit pattern. The signed shift right operator produce a result that equals to dividing a number by 2.

package org.kodejava.basic;

public class SignedRightShiftOperator {
    public static void main(String[] args) {
        // The binary representation of 32 is
        // "00000000000000000000000000100000"
        int number = 32;
        System.out.println("number       = " + number);
        System.out.println("binary       = " +
                Integer.toBinaryString(number));

        // Using the shift right operator we shift the bits
        // four times to the right which resulting the result
        // of "00000000000000000000000000000010"
        int shiftedRight = number >> 4;
        System.out.println("shiftedRight = " + shiftedRight);
        System.out.println("binary       = " +
                Integer.toBinaryString(shiftedRight));
    }
}

The result of the code snippet:

number       = 32
binary       = 100000
shiftedRight = 2
binary       = 10

How do I use the shift left “<<" operator?

The signed shift left operator << shifts a bit pattern to the left. This operator operates with two operand, the left-hand operand to be shifted and the right-hand operand tells how much position to shift.

Shifting a 0010 bit pattern using the << operator 2 position will produce a 1000 bit pattern. The signed shift left operator produce a result that equals to multiplying a number by 2, which double the value of a number.

package org.kodejava.basic;

public class SignedLeftShiftOperator {
    public static void main(String[] args) {
        // The binary representation of 2 is "0010"
        int number = 2;
        System.out.println("number      = " + number);
        System.out.println("binary      = " +
                Integer.toBinaryString(number));

        // Using the shift left operator we shift the bits two
        // times to the left. This will shift the "0010" into
        // "1000"
        int shiftedLeft = number << 2;
        System.out.println("shiftedLeft = " + shiftedLeft);
        System.out.println("binary      = " +
                Integer.toBinaryString(shiftedLeft));
    }
}

The result of the code snippet:

number      = 2
binary      = 10
shiftedLeft = 8
binary      = 1000

How do I use the unary bitwise complement “~” operator?

The unary bitwise complement operator (~) inverts a bit pattern; it can be applied to any of the integral types, making every 0 a 1 and every 1 a 0.

For example, an integer contains 32 bits; applying this operator to a value whose bit pattern is 00000000000000000000000000001000 would change its pattern to 11111111111111111111111111110111.

package org.kodejava.basic;

public class UnaryBitwiseComplementOperator {
    public static void main(String[] args) {
        // An integer type contains 32 bit information.
        // 8 = 00000000000000000000000000001000
        int number = 8;
        System.out.println("number = " + number);
        System.out.println(Integer.toBinaryString(number));

        // Using the ~ operator inverts the number by change the
        // every "0" to "1" and every "1" to "0".
        // 00000000000000000000000000001000
        // 11111111111111111111111111110111
        //
        int invertedNumber = ~number;
        System.out.println("invertedNumber = " + invertedNumber);
        System.out.println(Integer.toBinaryString(invertedNumber));
    }
}

The result of the code snippet:

number = 8
1000
invertedNumber = -9
11111111111111111111111111110111