How do I use the instanceof keyword?

To check whether an object is of a particular type (class or interface type) you can use instanceof operator. The instanceof operator is used only for object reference variable. x instanceof y can be read as x is-a y.

The instanceof returns true if the reference variable being tested is of the type being compared to. It will still return true if the object being compared is assignment compatible with the type on the right.

For interface type, an object is said to be of a particular interface type (meaning it will pass the instanceof test) if any of the object’s superclasses implement the interface.

package org.kodejava.basic;

interface Man {
}

public class InstanceofDemo {
    public static void main(String[] args) {
        Body body = new Body();
        Hand hand = new Hand();
        Nail nail = new Nail();
        Shoes shoe = new Shoes();

        if (body instanceof Man) {
            System.out.println("body is a Man");
        }

        if (hand instanceof Man) {
            System.out.println("hand is a Man too");
        }

        if (hand instanceof Body) {
            System.out.println("hand is a Body");
        }

        // it should be return false
        if (hand instanceof Nail) {
            System.out.println("hand is a Nail");
        } else {
            System.out.println("hand is not a Nail");
        }

        if (nail instanceof Man) {
            System.out.println("nail is a Man too");
        }

        if (nail instanceof Hand) {
            System.out.println("nail is a Hand");
        }
        if (nail instanceof Body) {
            System.out.println("nail is a Body too");
        }

        // it should return false, cause Shoes is not implements Man
        if (shoe instanceof Man) {
            System.out.println("shoe is a Man");
        } else {
            System.out.println("shoe is not a Man");
        }

        // compile error. cannot test against class in different
        // class hierarchies.
        //
        //if (shoe instanceof Body) {
        //}

    }

}

class Body implements Man {
}

// indirect implements Man
class Hand extends Body {
}

// indirect implements Man
class Nail extends Hand {
}

class Shoes {
}

The result of the code snippet above:

body is a Man
hand is a Man too
hand is a Body
hand is not a Nail
nail is a Man too
nail is a Hand
nail is a Body too
shoe is not a Man

How do I use the relational operator in Java?

Relational operators used to compare any combination of integers, floating-point numbers, or characters. The result of relational operators is always in a boolean value, true or false. It is mostly used in an if statement test.

There are four relational operators in Java:

  • > greater than
  • >= greater than or equal to
  • < less than
  • <= less than or equal to
package org.kodejava.basic;

public class RelationalDemo {
    public static void main(String[] args) {
        int value1 = 10, value2 = 25;
        int age = 15;
        double salary = 1000d;
        char char1 = 'd', char2 = 'f';

        if (value1 > value2) {
            System.out.format("%d is greater than %d %n", value1, value2);
        } else {
            System.out.format("%d is greater than %d %n", value2, value1);
        }

        if (age >= 12) {
            System.out.format("Hey, I am not a kid anymore %n");
        }

        if (char1 < char2) {
            System.out.format("%c is less than %c %n", char1, char2);
        } else {
            System.out.format("%c is less than %c %n", char2, char1);
        }

        if (salary <= 3000d) {
            System.out.println("Entry-level Staff");
        }
    }
}

An here are the result of the program:

25 is greater than 10 
Hey, I am not a kid anymore 
d is less than f 
Entry-level Staff

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 define a constant variable?

To define a constant in Java, use final modifier which combined with static modifier. The final modifier indicates that the value of this field cannot change.

If you change the value of the constant, you need to recompile the class to get the current value. Other feature in Java that provide similar functionality is enumeration (a list of named constants). You can simply create an enumeration by using the enum keyword.

package org.kodejava.example.fundamental;

public class ConstantDemo {
    public static void main(String[] args) {
        int sunday = DayConstant.SUNDAY;
        System.out.println("Sunday = " + sunday);

        String dozen = MeasureConstant.DOZEN;
        System.out.println("Dozen  = " + dozen);
    }
}

class DayConstant {
    public final static int SUNDAY = 0;
    public final static int MONDAY = 1;
    public final static int TUESDAY = 2;
    public final static int WEDNESDAY = 3;
    public final static int THURSDAY = 4;
    public final static int FRIDAY = 5;
    public final static int SATURDAY = 6;
}

class MeasureConstant {
    final static String UNIT = "unit";
    final static String DOZEN = "dozen";
}