Free Guides

Language Tutorials

                            

SUN

Your Ad Here

 
               SUN CERTIFIED JAVA PROGRAMMER FOR JAVA2 PLATFORM 1.4

       Index     

Operators and assignments

  • All binary operators except for relational and assignment operators associate from left to right. Relational operators are non-associative.
    All unary operators (except for unary postfix increment and decrement), all assignment operators, and the ternary conditional operator operate from right to left.

  • Precedence rules are used to determine which operator to apply first if there are two operators with different precedence, following each other in the expression. Associativity rules are used to determine which operator to apply first if there are two operators with the same precedence, following each other in the expression

 

  • Postfix

    [] . (parameters) (expr)++ (expr)--

    Unary prefix

    ++(expr) --(expr) +(expr) -(expr)

    Unary prefix creation and cast

    new (type)

    Multiplicative

    * / %

    Additive

    + -

    Shift

    << >> >>>

    Relational

    < <= > >= instanceof

    Equality

    == !=

    Bitwise/logical AND

    &

    Bitwise/logical XOR

    ^

    Bitwise/logical OR

    |

    Conditional AND

    &&

    Conditional OR

    ||

    conditional

    ?:

    Assignment

    = += -= *= /= %= <<= >>= >>>= &= ^= |=

  • Java guarantees that all operands of an operator are fully evaluated before the operator is applied. The only exceptions are &&, || and ?:. Also the operands of the operators are evaluated from left to right. For example, in the case of binary operators, if the evaluation of the left hand operator causes an exception, then the right hand expression is not evaluated.

  • Casting between primitives and references is not permitted. Boolean values cannot be cast to other data values and vice versa. null can be cast to any reference type.

  • All conversions between byte, short to char are considered narrowing, because conversion to the unsigned char can lead to loss in information.

  • Numeric Promotions

      • Unary Numeric Promotion : the operator is promoted to int if lesser than int.
        Examples : operands of +, -
        operand of ~
        during array creation, value in [ ]
        indexing array elements, value in [ ]
        operands of <<, >>, >>>

      • Binary Numeric Promotion : both operands are converted to the broadest of the two and to int at the least.
        Examples : operands of *, /, %, +, -
        operands of <, <=, >, >=
        operands of ==, !=
        operands of &, ^, |

  • Type Conversion can occur in the following contexts :

      • Assignments involving primitive data types and reference types.

      • Method invocation involving parameters of primitive data types.

      • Arithmetic expression evaluation involving numeric types.

      • String concatenation involving objects of String and other data types

  • The assignment statement is an expression statement, which returns the value of the expression on the right hand side.

  • Integer values when widened to floating point values may lead to loss of precision.

  • Implicit Narrowing Primitive Conversions take place when all of the following are true:

      • source is a constant expression of type byte, short, char or int.1

      • destination is byte, short or char type.

      • Value of source is determined to be in the range of the destination.

  • Integer division by 0 and remainder by 0 gives Arithmetic Exception.

  • Floating point errors throw NaN and Infinity, -Infinity.

  • (-0.0==0.0) is true
    (0.0/0.0) is NaN
    any operation involving
    NaN gives NaN
    any comparison involving
    NaN (except !=) gives NaN
    square root of negatives gives
    NaN
    to check if value is
    NaN, use isNaN(), defined in java.lang.Float and java.lang.Double.

  • In the case of Arithmetic Compound Assignment Operators (*=, /=, %=, +=, -=), since they have the lowest precedence of all operators in Java, the full expression on the right will always be evaluated before the assignment. Also the left hand side variable (also in the expansion on the right side) is evaluated only once.

  • Prefixed Increment/Decrement Operator first performs operation, then uses new value as result of the expression. Postfixed Increment/Decrement Operators use the current value as the result of the expression first and then perform the operation.

  • Increment and Decrement operators cannot be associated, like in (++(++a))since the inner increment does not result in a variable for the other increment to operate on. Similarly, Relational and Equality operators are not associative since first operation would result in a boolean which cannot be the operand of the second.

  • In the case of Object References, the Equality and the Inequality operators check whether the two references denote the same object or not. The state of the objects is not compared. To check for Object Value Equality, equals() method of class Object may be used, provided the class overrides the method. Otherwise Object Reference Equality and Object Value Equality are the same.2

  • Boolean Logical Operators can be applied to boolean operands to return a boolean value, or to integral operands to perform bitwise operations. But Conditional Operators can only be applied to boolean operands.

  • Adding 1 to the 1's complement of a positive binary integer gives its negative. And vice-versa. Hence if given a binary number and its signed bit is one, to find the decimal equivalent, we need to take the 2's complement, determine the decimal value and then affix a negative sign to it.

  • For the shift operator, a bit mask value of 0x1F (31, for int) or 0x3F (63, for long) is used by AND-ing with the shift distance value to ensure that the shift distance always stays in the range. (result of masking is <shiftvalue>%32 or 64).

  • The conditional operator can be nested. It is associative from right to left.

  • Formal parameters are those defined in the method definition. Actual parameters are those passed during the method call (which may vary from call to call).

      • The number of Actual parameters must equal the number of Formal parameters.

      • Individual Formal and Actual parameter pairs must be type-compatible.

  • For parameter passing there are no implicit narrowing conversions.

  • If an object reference is declared as final then it means that the reference value cannot be changed, however state of the object can be changed as usual.