DOC

Operators and Assignments(1)

By Vanessa Reyes,2014-09-17 15:22
9 views 0
Operators and Assignments(1)

    Operators and Assignments(1)

作者!jeru

email: jeru@163.net

日期!7/3/2001 9:55:32 AM

5)Operators and Assignments

Objective 1)

    Determine the result of applying any operator including assignment operators and instanceof to operands of any type class scope or accessibility or any combination of these.

1. Unary operators.

1.1 Increment and Decrement operators ++ --

We have postfix and prefix notation. In post-fix

    notation value of the variable/expression is modified after the value is taken for the execution of statement. In prefix notation, value of the variable/expression is modified before the value is

    taken for the execution of statement.

x = 5; y = 0; y = x++; Result will be x = 6, y = 5

x = 5; y = 0; y = ++x; Result will be x = 6, y = 6

    Implicit narrowing conversion is done, when applied to byte, short or char.

1.2 Unary minus and unary plus + -

+ has no effect than to stress positivity.

- negates an expressions value. (2s complement

    for integral expressions)

1.3 Negation !

Inverts the value of a boolean expression.

1.4 Complement ~

    Inverts the bit pattern of an integral expression. (1s complement 0s to 1s and 1s to 0s)

(~i=-i-1)

Cannot be applied to non-integral types.

1.5 Cast ()

    Persuades compiler to allow certain assignments. Extensive checking is done at compile and runtime to ensure type-safety.

2. Arithmetic operators - *, /, %, +, -

? Can be applied to all numeric types.

    ? Can be applied to only the numeric types, except + it can be applied to Strings as well.

    ? All arithmetic operations are done at least with int. (If types are smaller, promotion happens. Result will be of a type at least as wide as the wide type of operands)

    ? Accuracy is lost silently when arithmetic overflow/error occurs. Result is a nonsense value.

    ? Integer division or % by zero throws ArithmeticException.

    ? Floating point arithmetic always loses precision. The following code fragment returns false.

float f = 1.0 F / 3.0 F;

if ( A * 3.0 F == 1.0 F ) return true;

else return false;

    ? % - sign of the result entirely determined by sign of LHS

    ? Floating point calculations can produce NaN (square root of a negative no. or % by zero) or Infinity/-Infinity( division by zero). Float and Double wrapper classes have named constants for NaN

    and infinities.

    ? NaNs are non-ordinal for comparisons. X == Float.NaN wont work. Use Float.IsNaN(x) and Double.IsNaN(x). But equals method on wrapper objects(Double or Float) with NaN values compares Nans correctly.

? Infinities are ordinal. X ==

    Double.POSITIVE_INFINITY will give expected result.

    ? + also performs String concatenation (when any operand in an expression is a String). The language itself overloads this operator. toString() method of non-String object operands are called to perform

    concatenation. In case of primitives, a wrapper object is created with the primitive value and toString method of that object is called. (Vel +

    3 will work.)

    ? Be aware of associativity when multiple operands are involved.

System.out.println( 1 + 2 + 3 ); // Prints 33

System.out.println( 1 + 2 + 3 ); // Prints 123

3. Shift operators - <<, >>, >>>

    ? << performs a signed left shift. 0 bits are brought in from the right. Sign bit (MSB) is preserved.(?) Value becomes old value * 2 ^ x where

    x is no of bits shifted.

    ? >> performs a signed right shift. Sign bit is brought in from the left. (0 if positive, 1 if negative. Value becomes old value / 2 ^ x where x is no of bits shifted. Also called arithmetic right shift.

    ? >>> performs an unsigned logical right shift. 0 bits are brought in from the left. This operator exists since Java doesnt provide an unsigned data type (except char). >>> changes the sign of a negative number to be positive. So dont use it

    with negative numbers, if you want to preserve the sign. Also dont use it with types smaller than int. (Since types smaller than int are promoted to an int before any shift operation and the result is cast down again, so the end result is

    unpredictable.)

    ? Shift operators can be applied to only integral types.

? -1 >> 1 is 1, not 0.

    ? 1 << 31 will become the minimum value that an int can represent.

? Negative numbers are represented in twos

complement notation. (Take ones complement and add

    1 to get twos complement)

    ? Shift operators never shift more than the number of bits the type of result can have. ( i.e. int 32, long 64) RHS operand is reduced to RHS % x where x is no of bits in type of result.

int x;

x == x >> 32 // ture

    x = x >> 33; // Here actually what happens is x >> 1