Casting Incompatible Data Types
Casting Incompatible Data Types
Casting Incompatible Data Types
Average is 2.42.
Multidimensional arrays:
It is implemented as arrays of arrays.
Declaration: int twoD= new int[][];
When allocate memory -no need to specify all the
dimensions in the initialization itself. define the first
(left most) dimension then allocate the remaining
dimensions separately
No need to allocate the same number of elements for
each dimension (length of each array is under our
control)
Irregular array not appropriate -run contrary to
expectation irregular array might be perfect -need
large 2D array that is sparsely populated(not all the
elements will be used)
OPERATORS
Arithmetic operators
Bitwise operator
Relational operator
Boolean logical operator
Assignment operator
The ? operator Arithmetic compound Assignment operator
Adv: shorthand(less typing), more efficient
a = a + 4; equal to a += 4;
a = a % 2; equal to a %= 2;
x = 42;
y = ++x; equal to x = x + 1;
y = x;
y=43 (increment occurs before x is assigned y)
x = 42;
y = x++; equal to y=x;
x=x+1;
y=42(increment occurs after x is assigned to y
in both cases x is set 43.
absolute value of 10 is 10
absolute value of -10 is 10
Operator Precedence
Short-Circuit Logical Operator (conditional
Operators in the same row are equal precedence
AND,conditional OR
&&,||
Not bother to evaluate the right-hand operand when
the outcome can be determined by left hand
operand alone. It is useful when right hand depends
left hand for proper operation
if (denom != 0 && num / denom > 10)
when denom is zero-no run-time exception because
of &&, when &- both sides evaluated causing run-
time exception when denom=0.
Boolean logic use short-circuit AND and OR.
Bitwise operation -& single character version.
Using Parentheses:
a >> b + 3 rewritten as a >> (b + 3)[ first adds 3 to b
and then shifts a right by that result]
(a >> b) + 3 [first shift a right by b positions and then
add 3 to that result]
a | 4 + c >> b & 7
(a | (((4 + c) >> b) & 7)) //easy to read
parentheses (redundant or not) do not degrade the
performance of your program. Adding parentheses
to reduce ambiguity does not negatively affect your
program.