By HP KINGDOM |
Operators in Java
Operators in Java
Java provides many types of operators which can be used according to the need. They are classified based on the functionality they provide. Some of the types- Unary Operator,
- Arithmetic Operator,
- Shift Operator,
- Relational Operator,
- Bitwise Operator,
- Logical Operator,
- Ternary Operator and
- Assignment Operator.
Java Operator Precedence
Operator Type | Category | Precedence |
---|---|---|
Unary | postfix | expr expr-- |
prefix | expr expr expr expr ~ ! |
|
Arithmetic | multiplicative | * / % |
additive | + - |
|
Shift | shift | << >> >>> |
Relational | comparison | < > <= >= instanceof |
equality | == != |
|
Bitwise | bitwise AND | & |
bitwise exclusive OR | ^ |
|
bitwise inclusive OR | | |
|
Logical | logical AND | && |
logical OR | || |
|
Ternary | ternary | ? : |
Assignment | assignment | = += -= *= /= %= &= ^= |= <<= >>= >>>= |
Java Unary Operator
The Java unary operators require only one operand. Unary operators are used to perform various operations i.e.:- incrementing/decrementing a value by one
- negating an expression
- inverting the value of a boolean
- – :Unary minus, used for negating the values.
- + :Unary plus, indicates positive value (numbers are positive without this, however). It performs an automatic conversion to int when the type of its operand is byte, char, or short. This is called unary numeric promotion.
- ++ :Increment operator, used for incrementing the value by 1. There are two varieties of increment operator.
- Post-Increment : Value is first used for computing the result and then incremented.
- Pre-Increment : Value is incremented first and then result is computed.
- — : Decrement operator, used for decrementing the value by 1. There are two varieties of decrement operator.
- Post-decrement : Value is first used for computing the result and then decremented.
- Pre-Decrement : Value is decremented first and then result is computed.
- ! : Logical not operator, used for inverting a boolean value.
Java Unary Operator Example: ++ and --
10 12 12 10
Java Unary Operator Example 2: ++ and --
22 21
Java Unary Operator Example: ~ and !
class OperatorExample{
public static void main(String args[]){
int a=10;
int b=-10;
boolean c=true;
boolean d=false;
System.out.println(~a);//-11 (minus of total positive value which starts from 0)
System.out.println(~b);//9 (positive of total minus, positive starts from 0)
System.out.println(!c);//false (opposite of boolean value)
System.out.println(!d);//true
}}
-11 9 false true
Java Arithmetic Operators
Java arithmatic operators are used to perform addition, subtraction, multiplication, and division. They act as basic mathematical operations. They are used to perform simple arithmetic operations on primitive data types.- * : Multiplication
- / : Division
- % : Modulo
- + : Addition
- – : Subtraction
Java Arithmetic Operator Example
class OperatorExample{ public static void main(String args[]){ int a=10; int b=5; System.out.println(a+b);//15 System.out.println(a-b);//5 System.out.println(a*b);//50 System.out.println(a/b);//2 System.out.println(a%b);//0 }}
15 5 50 2 0
Java Arithmetic Operator Example: Expression
class OperatorExample{
public static void main(String args[]){
System.out.println(10*10/5+3-1*4/2);
}}
Output:
21
Java Left Shift Operator
The Java left shift operator << is used to shift all of the bits in a value to the left side of a specified number of times.Java Left Shift Operator Example
class OperatorExample{ public static void main(String args[]){ System.out.println(10<<2);//10*2^2=10*4=40 System.out.println(10<<3);//10*2^3=10*8=80 System.out.println(20<<2);//20*2^2=20*4=80 System.out.println(15<<4);//15*2^4=15*16=240 }}
40 80 80 240
Java Right Shift Operator
The Java right shift operator >> is used to move left operands value to right by the number of bits specified by the right operand.Java Right Shift Operator Example
2 5 2
Java Shift Operator Example: >> vs >>>
class OperatorExample{
public static void main(String args[]){
System.out.println(10<<2);//10*2^2=10*4=40
System.out.println(10<<3);//10*2^3=10*8=80
System.out.println(20<<2);//20*2^2=20*4=80
System.out.println(15<<4);//15*2^4=15*16=240
}}
5 5 -5 1073741819
Java AND Operator Example: Logical && and 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.false false
Java AND Operator Example: Logical && vs Bitwise &
false 10 false 11
Java OR Operator Example: Logical || and Bitwise |
The logical || operator doesn't check second condition if first condition is true. It checks second condition only if first one is false. The bitwise | operator always checks both conditions whether first condition is true or false.true true true 10 true 11
Java Ternary Operator
Java Ternary operator is used as one liner replacement for if-then-else statement and used a lot in Java programming. it is the only conditional operator which takes three operands.Java Ternary Operator Example
class OperatorExample{
public static void main(String args[]){
int a=2;
int b=5;
int min=(a<b)?a:b;
System.out.println(min);
}}
2
5
Java Assignment Operator
Java assignment operator is one of the most common operator. It is used to assign the value on its right to the operand on its left.Java Assignment Operator Example
14 16
Java Assignment Operator Example
class OperatorExample{
public static void main(String[] args){
int a=10;
a+=3;//10+3
System.out.println(a);
a-=4;//13-4
System.out.println(a);
a*=2;//9*2
System.out.println(a);
a/=2;//18/2
System.out.println(a);
}}
13 9 18 9
Java Assignment Operator Example: Adding short
Compile time error
-
class OperatorExample{
-
public static void main(String args[]){
-
short a=10;
-
short b=10;
-
a=(short)(a+b);//20 which is int now converted to short
-
System.out.println(a);
-
}}
20