Operators precedence?
Post unary -> exp++, exp—
Pre unary -> ++exp, —exp
Other unary -> -, !, ~, +, (type) RL*
Cast -> (Type) reference RL*
Multiply/divide/modulus -> , /, %
Add/subtract -> +, -
Shift -> «,»,»>
Relational -> <, >, <=, >=, instanceof
Equal to/not equal -> ==, !=
Logical AND -> &
Logical exclusive OR -> ^
Logical inclusive OR -> |
Conditional AND -> &&
Conditional OR -> ||
Ternary -> exp?exp1:exp2 **RL
Assignment -> =, +=, -=, =, /=, %=, &=, ^=, |=, «=,»_space;=,»_space;>= **RL
Arrow -> -> ***RL
Can we use brackets instead of parentheses in expressions?
No. Brackets are not recognized in expressions and will result in compile error when used
What is the result of below
-7%-5
-2
Negative sign on the divisor is always ignored when modulus is used.
Numeric promotion
1) if 2 values are different data types, Java will automatically promotes smaller variable to higher datatype
2) if integral and floating point are combined in an expression, Java will automatically promotes integral value to floating
3) if smaller data types such as byte, short and char are used in binary arithmetic expression, Java will promote them to int. Even in case that both operands are byte, short or char
4) resulting value has the promoted datatype
Is this valid?
long x = 10;
Int y = 2;
y = x * y;
No.
Y is promoted to long and result is 20 which is long. Result is being assigned to an int without casting.
Is this valid?
long x = 10;
Int y = 2;
y *= x;
Yes.
In this case y on right side is promoted to long and the result is long 20 but result is implicitly cast to int.
Special scenario in compound assignment operation
Is this valid? What it evaluates to?
String name=null;
name instanceof Object
null instanceof Object
Compiles fine.
It returns false. null is being as an object reference still
What this evaluates to
String title = “Venky”
title instanceof null
Doesn’t compile.
Compiler thinks it’s foolish to even check a reference to be instance of null (not being a class)
What this evaluates to?
Integer x=20;
x instanceof Date
Doesn’t compile.
Since compiler knows x being an integer can’t possible be a Date
Different categories of operators
1)Unary
2)Binary (arithmetic, relational, logical, conditional)
3)Ternary (compound and lambda)
Logical operators?
Logical AND - &
Logical OR - |
Logical exclusive OR - ^
They act like conditional operators when applied on boolean values.
When applied on numerical data types they act like bitwise operators.
What is the value of y
Int x=102
Int y=~x
What is the value of z
Int z = x | y
What is the value of z
Int z = x & y
What is the value of z
Int z = x ^ y
-103
-1
0
-1
Conditional operators
&&
||
What is the output
Int rabbit=6;
Boolean bunny= (rabbit>=6) || (++rabbit <=7);
System.out.println(bunny);
System.out.println(rabbit);
true
6
What is the output
Int count=3;
Int x=count < 5? 4 : “More”;
System.out.println(x);
Doesn’t compile
Because compiler knows there is a possibility that count may be greater than 5 and evaluating to string which cannot be put in integer data type
What data types are allowed in the blank
byte apples=5;
Short oranges=7;
________ bananas = apples + oranges;
int
long
float
double
Assignment operators
long a = 10;
a += 3.2
long b = 23:
b = b * 1.0;
What is the value of a and b
a=13
b Doesn’t compile
When it comes to assignment operators such as this , it requires an explicit type casting.
What is the values of a and b at the end of the code block?
Int a=2;
Int b=4;
b += 2 + a++;
a *= 3;
b += (long) 2;
a=9
b=10
What is the value of a?
Int a = (byte)(Byte.MAX_VALUE + 1)
-128
The range is
-128, -127……. 0, 1, ……… 127
When you add 1 to 127 it just wraps around to -128. Imagine it as a loop.