0% found this document useful (0 votes)
12 views

Operators

The document discusses the different types of operators in Java including arithmetic, relational, bitwise, logical, increment/decrement, shift, ternary, and assignment operators. It provides examples to explain the usage and behavior of each operator type.

Uploaded by

sb349643
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Operators

The document discusses the different types of operators in Java including arithmetic, relational, bitwise, logical, increment/decrement, shift, ternary, and assignment operators. It provides examples to explain the usage and behavior of each operator type.

Uploaded by

sb349643
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Operators in Java

It is a symbol which is used to perform operations. There are many types of operators in Java which
are given below:
• Arithmetic Operator
• Relational Operator
• Bitwise Operator
• Logical Operator
• Increment and Decrement
• Shift Operator
• Ternary Operator
• Assignment Operator.

Java Operator Precedence


Operator Type Category Precedence
Arithmetic Multiplicative * / %
Additive + -
Relational Comparison < > <= >=
instanceof
Equality == !=
Bitwise bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
Logical logical AND &&
logical OR ||
Increment and Postfix expr++ expr--
Decrement
Prefix ++expr --expr
Shift Shift << >> >>>
Ternary Ternary ? :
Assignment Assignment = += -= *= /=
%= &= ^= |= <<=
>>= >>>=

1. Arithmetic Operators: These are used to perform addition, subtraction, multiplication, and
division.
Example 1:
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
}
}

Output: 15
5
50
2
0
Example 2:
class OperatorExample
{
public static void main(String args[])
{
System.out.println(10 * 10 / 5 + 3 – 1 * 4 / 2);
}
}

Output: 21

2. AND and OR Operator:


a) 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.

Example 3:
class OperatorExample
{
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
System.out.println(a < b & a < c); // false & true = false
//&& vs &
System.out.println(a < b && a++ < c); //false && true = false
System.out.println(a); //10 because second condition is not checked
System.out.println(a < b & a++ < c); //false && true = false
System.out.println(a); //11 because second condition is checked
}
}

Output: false
false
false
10
false
11

b) 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.

Example 4:
class OperatorExample
{
public static void main(String args[])
{
int a = 10;
int b = 5;
int c = 20;
System.out.println(a > b || a < c); //true || true = true
System.out.println(a > b | a < c); //true | true = true
//|| vs |
System.out.println(a > b || a++ < c); //true || true = true
System.out.println(a); //10 because second condition is not checked
System.out.println(a > b | a++ < c); //true | true = true
System.out.println(a); //11 because second condition is checked
}
}

Output: true
true
true
10
true
11

3. Increment and Decrement Operator: These are used to incrementing/decrementing a value


by one.
Example 5:
class OperatorExample
{
public static void main(String args[])
{
int x = 10;
System.out.println(x++); //10 (11)
System.out.println(++x); //12
System.out.println(x--); //12 (11)
System.out.println(--x); //10
}
}

Output: 10
12
12
10

4. Shift Operator:
a) Left Shift Operator(<<): It is used to shift all of the bits in a value to the left side of a specified
number of times.
Example 6:
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
}
}

Output: 40
80
80
240

b) Right Shift Operator(>>): It is used to move left operands value to right by the number of
bits specified by the right operand.
Example 7:
class OperatorExample
{
public static void main(String args[])
{
System.out.println(10 >> 2); //10/2^2=10/4=2
System.out.println(20 >> 2); //20/2^2=20/4=5
System.out.println(20 >> 3); //20/2^3=20/8=2
}
}

Output: 2
5
2

Example 8: >> vs >>>


class OperatorExample
{
public static void main(String args[])
{
System.out.println(20 >> 2); //For positive number, >> and >>> works same
System.out.println(20 >>> 2);
//For negative number, >>> changes parity bit (MSB) to 0
System.out.println(-20 >> 2);
System.out.println(-20 >>> 2);
}
}
Output: 5
5
-5
1073741819

5. Java Ternary Operator: It is the only conditional operator which takes three operands.
Example 9:
class OperatorExample
{
public static void main(String args[])
{
int a = 2;
int b = 5;
int min = (a < b) ? a : b;
System.out.println(min);
}
}

Output: 2

6. Java Assignment Operator: It is used to assign the value on its right to the operand on its left.
Example 10:
class OperatorExample
{
public static void main(String args[])
{
int a = 10;
int b = 20;
a + = 4; //a = a + 4 (a = 10 + 4)
b - = 4; //b = b - 4 (b = 20 - 4)
System.out.println(a);
System.out.println(b);
}
}

Output: 14
16

You might also like