SCJP Study Notes


Operators and Assignments

  • Integer division and % will generate an ArithmeticException when divide by zero. No other arithmetic results an Exception,rather results in an unexpected value.
  • Java does not allow operator overloading.
  • The >> and >>> operators yield the same result when a number is positive.
  • Conditional operators have higher precedence than assignment operator.
  • Java allows you to use ~ operator for integer type variables. The simple way to calculate is ~i = (- i) - 1.
  • The unsigned right shift operator >>> results always in a positive number.
  • || and && operators are known as short circuit logic operators.
  • a=x ? b:c; the type of the expression x should be boolean. The types of the expressions b and c should be compatible and are made identical through conversion.
  • byte b=2;
    b=b+2; // does not compile
  • byte b=2;
    b+=2; // compiles
  • the instanceof operator generates a boolean value.
  • if the left argument of instanceof operator is null, simply returns false not an exception.
  • == and != should not be used to compare the contents of objects.
  • The Object class' equals() method just does == check.
  • The compiler will implicitly do a narrowing conversion for an assignment statement if the right hand operand is a compile time constant of type byte, short, char, or int
    and the value falls within the range of the variable on the left and if the variable is of type byte, short, or char.
  • Short is signed and char is not signed so an explicit cast is necessary when a short is assigned to a char and vice versa.
  • The precedence of the cast operator is higher than the precedence of the addition operator

contents | previous | next