Chapter4
Un article de Sometimes Kitties Think Too.
Sommaire |
[modifier]
Chapter 4 - Operators
+ Java operators are not usually overloaded except '+' and '&', '|', '^'
(meaning they are used as bitwise operators and for boolean tests)
+ assignment operators get evaluated RIGHT to LEFT (right associative)
+ operands are evaluated LEFT to RIGHT
[modifier]
Compound Operators
+ compound operators: +=, -=. *=. /=
+ expression on the r.h.s. will be evaluated first
e.g. x += 2 + 5
x = x + (2 + 5)
[modifier]
Relationial Operators
+ <, , >, >=, ==, !=
+ result in true/false value
+ when using characeters for comparison, the Unicode value will be used
[modifier]
Equality Operators
+ ==, !=, return a boolean value
+ disimilar types cannot be compared
+ only numbers, characters, boolean primatives and object
reference variables can be compared
+ floating point operator is equal to an integer???? 5.0 == 5L, true
+ reference variables are equal if they refer to the same thing
+ result of an boolean assignment is boolean value
e.g. if ( b = false) { } // the condition will be false
+ result of an integer assignment is int value
e.g. if (x = true) --> true
if (x = 0) --> 0, not legal
+ when comparing enums you can use '==' and .equals()
+ instanceof used for reference variables only, IS-A test
+ instanceof will return true when an object is tested against
it's own type or any supertypes
+ instanceof returns true when type matches an interface of any supertype
+ 'null' is not an instanceof any class
[modifier]
Arithmetic Operators
+ '%' remainder operator
+ *,/,% have higher precedence than +,-
[modifier]
String Concatenation
+ if either operand is a String, the '+' sign becomes a concatenator
[modifier]
Increment/Decrement Operators
+ '--', '++' can be prefix or postfix
+ cannot operate on final variables
[modifier]
Conditional Operator
+ ternary (3 operands) used to evaluate boolean expressions
+ (...) ? (...) : (...)
[modifier]
Logical Operators
+ bitwise: |,&,^
+ short-circuit (&&, ||)
evaluate statements containing 1+ boolean expressions
+ non-short-circuit ( &, |)
which evaluate both sides of the expression
+ non-bitwise: (^, !)
XOR - exactly one expression must be true
[modifier]
Shift Operators
<<, >>, >>>
>>> - unsigned right shift operator
[modifier]
Operator Precedence
in ranking from highest to lowest
----
Parantheses, Array
[], (), . (member access)
----
Increment,Decrement
++, -- (prefix) --> ++,-- (postfix) --> - (unary)
----
Multiplicative, Additive
/,*,% --> +,-
----
Relational
<,>, >=, <=
----
Equality
==, !=
----
And,Or, Conditional
&& --> || --> ?:
----
Assignment
=, +=, -=, *=, /=, %=
