Please enable JavaScript.
Coggle requires JavaScript to display documents.
Understanding Java Operators (Unary Operators (Increment and Decrement…
Understanding Java Operators
Special symbols
applied set of variables, values, or literals
unary, binary and ternary
Operator
Pre-unary operator
++expression
--expression
Post-unary operator:
expression--;
expression++;
Operator modulus (%)
negative integer dividend
result of remainder will be between (-y + 1) à 0
y is divisor
positive integer dividend
result of remainder will be between 0 à (y - 1)
y is divisor
e.g 10 % 3 = 1
Numeric promotion rules
convertion to large type
convertion to float-point
byte, char, short promoted to int
result will have the same data type promoted
Examples
int x; long y;
x * y
x promoted to long
1 rule
double x; float y;
x + y
y promoted to double
1 rule
short x; short y;
x / y;
x and y promoted to int
3 rule
short x; float y; double z;
x promoted int
x promoted float
x * y promoted double
all rules
Binary Arithmetic Operators
mathematical operator
logical expressions
basic variable assignment
Unary Operators
required one operand or variable
+
Indicate positive number
-
Indicate negative number
++
Increment 1
--
Decrement 1
!
Invert Boolean's logical value
Logical Complement Operator
!
e.g x = true;
x = !x; //x = false
Negation operator
-
e.g int x = 1.21;
x = -x; //-1.21
Tip
int x = !5;//Does not compile
boolean y = -true; //Does not compile
boolean z = !0; //Does not compile
Increment and Decrement Operators
Pre
operator and after return value
e.g ++counter;
--counter;
Post
return value and after operator
e.g counter++;
counter--;
Example
int x = 3;
int y = ++x * 5 / x-- + --x;
result x = 2; y = 7;
operators occur by left-hand side
Operator assignment
=
e.g int x = (int) 1.0;
casting double to int
Casting primitive values
occur overflow or underflow
e.g overflow short s = 1_922_222;
s will be 20.678.
e.g underflow int i = 2_147_483_647 +1;
i will be -2147483648
Compound assignment operator
+=
-=
*=
/=
cast withou explicity
e.g long x = 10; int y = 5;
y *= x; //Compile
y = y * x;//Does not compile
Relational Operators
References object
a instanceof b
True if reference point to class, subclass or implements interface
out of scope of OCA exam
primitive values
>, <, <=, >=
e.g int x = 10, z = 11;
x < z //true
Return a boolean value
Logical Operators
logical operators
&, |, ^
& And. Out true if both true
| Inclusive OR. Out false if both false
^ Exclusive OR. Out true if both different
bitwise operators
Compare bits from numeric values
short-circuit operators
&&, ||, ^^
e.g if(x != null && x.getValue() < 5)
x null, right-hand side will never be evaluated
e.g if(x!= null & x.getValue() <5)
x null, throw an exception, right-hand side was evaluated
tip
e.g int x = 6;
boolean y = (x >= 6) || (++x <= 7)
out x will be 6
Equality Operators
operator equal
==
operator not equal
!=
used three scenarios
1- Comparing two numeric primtive types
Comparing two booleans value
Comparing two Objects, including null and String values
tip
pay attention in the data types when using equality
e.g File x = new File("mytext.txt");
File z = x;
x == z is true