Java Operators

Java operators are symbols that allow you to perform various operations on variables and values. These operators are classified into different categories, some of which are:

  • Unary Operator,
  • Arithmetic Operator,
  • Shift Operator,
  • Relational Operator,
  • Bitwise Operator,
  • Logical Operator,
  • Ternary Operator and
  • Assignment Operator.

Precedence of Operators in Java:

Operator Type Category Precedence
Unary postfix expr++ expr–
prefix ++expr –expr +expr -expr ~ !
Arithmetic multiplicative * / %
additive + –
Shift shift << >> >>>
Relational comparison < > <= >= instance of
equality == !=
Bitwise bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
Logical logical AND &&
logical OR ||
Ternary ternary ? :
Assignment assignment = += -= *= /= %= &= ^=

Java Unary Operator

Unary operators in Java work with only one operand. They are used to perform a variety of operations, including incrementing or decrementing a value by one, negating an expression, and inverting the value of a boolean. These operators are important in Java because they allow developers to modify and manipulate variables and expressions in different ways. Some common examples of Java unary operators are the increment operator (++), decrement operator (–), negative operator (-), and logical complement operator (!).

Java Unary Operator Example: ++ and —

public class OperatorExample{  
public static void main(String args[]){  
int x=20;  
System.out.println(x++);//20 (11)  
System.out.println(++x);//22  
System.out.println(x--);//22 (11)  
System.out.println(--x);//20  
}}

Output:

20
22
22
20

Java Unary Operator Example: ~ and !

public class OperatorExample{  
public static void main(String args[]){  
int a=70;  
int b=-70;  
boolean c=true;  
boolean d=false;  
System.out.println(~a);//-71 (minus of total positive value which starts from 0)  
System.out.println(~b);//69 (positive of total minus, positive starts from 0)  
System.out.println(!c);//false (opposite of boolean value)  
System.out.println(!d);//true  
}}

Output:

-71
69
false
true

Java Arithmetic Operators

Arithmetic operators in Java are fundamental mathematical operations that perform addition, subtraction, multiplication, and division. They are used to manipulate numeric values in Java programs.

Java Arithmetic Operator Example Program:

public class OperatorExample{  
public static void main(String args[]){  
int a=80;  
int b=5;  
System.out.println(a+b);//85  
System.out.println(a-b);//75  
System.out.println(a*b);//400  
System.out.println(a/b);//16  
System.out.println(a%b);//0  
}}

Output:

80
75
400
16
0

Java Arithmetic Operators Expression Program:

public class OperatorExpression{  
public static void main(String args[]){  
System.out.println(10*10/5+3-1*4/2);  
}}

Output:

21

Java Left Shift Operator

In Java, the left shift operator “<<” is used to shift all the bits of a given value towards the left side by a specified number of positions. The left shift operator is a bitwise operator, which means that it operates on each bit of the given value.

Java Left Shift Operator Program Example

public 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

Java Right Shift Operator

The Java right shift operator >> is an operator used to shift the bits of the left operand to the right by a specified number of times, as determined by the right operand. It is used to perform division by a power of two on signed and unsigned numbers.

Java Right Shift Operator Program Example

public 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

Java Shift Operator Program Example: >> vs >>>

public class OperatorExample{  
public static void main(String args[]){  
    //For positive number, >> and >>> works same  
    System.out.println(20>>2);  
    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

Java AND Operator: Logical && and Bitwise &

The behavior of the logical AND (&&) and bitwise AND (&) operators in Java is different.

The logical AND operator only evaluates the second condition if the first condition is true. This is because if the first condition is false, the overall result of the AND operation will always be false, regardless of the second condition’s value. This is known as “short-circuiting” and can be used for optimization and to avoid unnecessary computations.

On the other hand, the bitwise AND operator always checks both conditions, regardless of the first condition’s value. This is because the bitwise AND operator performs a bitwise operation on each corresponding bit of the operands, and each bit needs to be evaluated to produce the correct result.

public 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  
}}

Output:

false
false

Java AND Operator Example: Logical && vs Bitwise &

public 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);//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
10
false
11

Java OR Operator Example: Logical || and Bitwise |

The logical OR operator (||) in Java only checks the second condition if the first one is false. This means that if the first condition evaluates to true, the second condition is skipped. On the other hand, the bitwise OR operator (|) always checks both conditions regardless of the evaluation of the first condition. This means that both the first and the second conditions are evaluated even if the first condition is true.

public 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

Java Ternary Operator

The Java Ternary operator is a shorthand notation that can replace an if-then-else statement with a single line of code. It is commonly used in Java programming to simplify code and improve readability. The Ternary operator is unique in that it takes three operands, hence the name. It is used to evaluate a boolean expression and return one value if the expression is true, and another value if the expression is false.

Example:

public 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

Java Assignment Operator

The Java assignment operator is a fundamental operation that is extensively used in Java programming. It is used to assign the value on the right side of the operator to the operand on its left side. This operator is denoted by the equal sign (=).

Java Assignment Operator Program

public class AssignmentOperator{  
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

Java Assignment Operator Adding short

public class AddingShort{  
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);  
}}

Output:

20

We hope you have gained a proper understanding of Java operators through this article. To continue expanding your knowledge, make sure to follow tutorials.freshersnow.com for daily updates.