Operators and Assignments(2)

By Lauren Torres,2014-09-17 15:23
9 views 0
Operators and Assignments(2)

    Operators and Assignments(2)



日期,7/3/2001 9:56:15 AM

Objective 2)

    Determine the result of applying the boolean equals(Object) method to objects of any combination of the classes java.lang.String java.lang.Boolean and java.lang.Object.

The equals method can be considered to perform a

    deep comparison of the value of an object, whereas the == operator performs a shallow comparison. The equals method compares what an object points to rather than the pointer itself (if we can admit that Java has pointers).

The equals method applied to a String, however that

    String was created, performs a character by character comparison.

Objective 3)

    In an expression involving the operators & | && || and variables of known values state which operands are evaluated and the value of the expression.

Objective 4)

    Determine the effect upon objects and primitive values of passing variables into methods and performing assignments or other modifying operations in that method.

Unary Numeric Promotion


? Operand of the unary arithmetic operators +


? Operand of the unary integer bit-wise

    complement operator ~

    ? During array creation, for example new int[x], where the dimension expression x must evaluate to an int value.

    ? Indexing array elements, for example table[a], where the index expression must evaluate to an int value.

? Individual operands of the shift operators.

Binary numeric promotion


    ? Operands of arithmetic operators *, / , %, + and

    ? Operands of relational operators <, <= , > and >=

    ? Numeric Operands of equality operators ==

and !=

    ? Integer Operands of bit-wise operators &, ^ and |

Conversion of Primitives

    1. 3 types of conversion assignment conversion, method call conversion and arithmetic


    2. boolean may not be converted to/from any non-boolean type.

3. Widening conversions accepted. Narrowing

    conversions rejected.

4. byte, short cant be converted to char and

    vice versa.

5. Arithmetic promotion

5.1 Unary operators

? if the operand is byte, short or char {

convert it to int;


     else {

do nothing; no conversion needed;


5.2 Binary operators

? if one operand is double {

    all double; convert the other operand to double;


     else if one operand is float {

all float; convert the other operand to float;


else if one operand is long {

all long; convert the other operand to long;


else {

all int; convert all to int;


    6. When assigning a literal value to a variable, the range of the variables data type is checked

    against the value of the literal and assignment is allowed or compiler will produce an error.

    char c = 3; // this will compile, even though a numeric literal is by default an int since the range of char will accept the value

int a = 3;

char d = a; // this wont compile, since were

    assigning an int to char

char e = -1; // this also wont compile, since the

    value is not in the range of char

float f = 1.3; // this wont compile, even though

    the value is within float range. Here range is not important, but precision is. 1.3 is by default a double, so a specific cast or f = 1.3f will work.

    float f = 1/3; // this will compile, since RHS evaluates to an int.

float f = 1.0 / 3.0; // this wont compile, since

    RHS evaluates to a double.

    7. Also when assigning a final variable to a variable, even if the final variables data type is

    wider than the variable, if the value is within the range of the variable an implicit conversion is


byte b;

final int a = 10;

b = a; // Legal, since value of a is

    determinable and within range of b

final int x = a;

b = x; // Legal, since value of x is

    determinable and within range of b

int y;