My Notes on Java Operators..

Summary from OCA/OCP Java SE 7 Programmer I & II Study Guide (Exams 1Z0-803 & 1Z0-804) (Certification Press)!
  • When using a compound operator; the expression on the right side of the "=" will always be evaluated first.
int x = 6;
x *= 2 + 5;
x = x * (2 + 5);
x = 42
  • Java has 6 relational operators: >, <, >=, <=, ==, !=.
  • The result of any assignment expression in Java is the result of the assignment itself.
  • equals() method in class Object acts same with the equals operator (==). If two references point to the same object, the equals method returns true.
    • The equals() method should be overwritten and used to determine if two objects are "meaningfully equal".
  • The equals() method in the String class is overriden and compares the values of the String. 
    • String#equals returns true if the Strings have the same value, not if the references point to the same object.
  • The equality operator (==) and the equals() method work same for enums.
MyEnums fooOne = MyEnums.FOO;
MyEnums fooTwo = MyEnums.FOO;
System.out.println(fooOne == fooTwo); // true
System.out.println(fooOne.equals(fooTwo)); // true
  • instanceof operator can only compare reference variables to types and the type of the reference variable must be in the inheritance tree of the type being compared to.
class Foo {}
class Bar extends Foo {}
class Main {
    void fooBar() {
        final Foo foo = new Bar();
        final Bar bar = new Bar();
        System.out.println(foo instanceof Foo); // true
        System.out.println(bar instanceof Foo); // true
        System.out.println(foo instanceof Object); // true
        final Foo baz = new Foo();
        System.out.println(baz instanceof Bar); // legal but false
        final Foo nullFoo = null;
        System.out.println(nullFoo instanceof Bar); // legal but false
        System.out.println(foo instanceof String); // will not even compile!
  • An array is always an instance of an Object, even if the array is an array of primitives..
int[] foo = new int[0];
// foo instanceof Object will return true
  • The remainder operator (%), a.k.a. the modulus operator, divides the left operand with the right operand and the result is the remainder of the operation.
  • There are 2 operators that will either increment or decrement a value by exactly 1.. 
    • '++''  
    • '--'.
      • foo++ is post-incremented, meaning foo is used in the statement first then it is incremented.
      • ++foo is pre-incremented, meaning foo is incremented first, then used in the statement.
int x = 10;
System.out.println(x++); // prints 10
System.out.println(++x); // prints 12
  • Ternary operator, ?, assigns a value to a variable if the tested condition is true.
class Main {
    public static void main(final String[] args) {
        System.out.println(max(5, 15));
        System.out.println(max(15, 5));
        System.out.println(max(5, 5));
    static int max(final int y, final int x) {
        final int max;
        max = y > x ? y : x;
        return max;
  • && and || are called short-circuit and and short-circuit or operators.
    • They operate on boolean variables or expressions that return booleans.
    • If the second operand is an expression, the second operand is only evaluated if the first operand is not enough to resolve the complete operation.
      • If the first operand is true or evaluates to true in a short-circuit or operation, the second operand is ignored.
      • If the first operand is false or evaluates to false in a short-circuit and operation, the second operand is ignored.
if (foo != null && foo.equals(bar)) {} 
// if foo is null, foo.equals() will never be called, thus NPE is avoided!
  • & and | are called non-short-circuit logical operators
    • They always check both operators! Always..
  • The xor operator, ^, also known as the 'Exclusive Or', always evaluates both sides and returns true only if one of the operands are true.