Acharya Parambarai
Acharya Parambarai
Additive +-
Equality == !=
bitwise exclusive OR ^
bitwise inclusive OR |
logical OR ||
Ternary Ternary ?:
1) Increment Operator - ++
int a=5
++ a ( it increment one value to the variable a) now a= 6
2) Decrement Operator - - -
int a=5
- -a ( it decrement one value to the variable a) now a= 4
Types
Pre increment / pre decrement
Post increment / Post decrement
3) Minus Operator -
int a=5
Negative value of the a now a= -5
class BinaryLogicalAnd
{
public static void main(String args[]){
int a=10;
int b=5;
int c=20;
System.out.println(a<b&&a<c); //false && true = false
//10<5 && 10 < 20
// False && True = False
System.out.println(a<b&a<c); //false & true = false
}
}
5) Java AND Operator Example: Logical && vs Bitwise &
The logical && operator doesn't check second condition if first condition is false. It checks
second condition only if first one is true.
The bitwise & operator always checks both conditions whether first condition is true or false.
}
6) Assignment and Short hand operator
Short hand operator
a = a+ b ( equal to) a+=b
a< b ? a : b
2< 5 ? 2 : 5
True : 2 : 5
Precedence and associativity are two features of Java operators. When there are two or more
operators in an expression, the operator with the highest priority will be executed first.
Associativity specifies the order in which operators are executed, which can be left to right or
right to left. For example, in the phrase a = b = c = 8, the assignment operator is used from
right to left. It means that the value 8 is assigned to c, then c is assigned to b, and at last b is
assigned to a. This phrase can be parenthesized as (a = (b = (c = 8)).
The priority of a Java operator can be modified by putting parenthesis around the lower order
priority operator, but not the associativity. In the equation (1 + 2) * 3, for example, the addition
will be performed first since parentheses take precedence over the multiplication operator.
The operator's precedence refers to the order in which operators are evaluated within an
expression whereas associativity refers to the order in which the consecutive operators within
the same group are carried out.
Precedence rules specify the priority (which operators will be evaluated first) of operators.
class Precedence {
public static void main(String[] args)
{
int a = 10, b = 5, c = 1, result;
result = a-++c-++b;
System.out.println(result);
}
}
Output: