Hello everyone, in the previous topic we were talking about **Ratan Tata**. Today we are going to talk about the **Operators in Java**.

Contents

# Operators in Java

Following are the types of operators in Java-

## Arithmetic Operator

They are used to performing simple arithmetic operations on primitive data types.

* | Multiplication |

/ | Division |

% | Modulo |

+ | Addition |

_ | Subtraction |

Java Arithmetic Operator Example is given below-

class test{ 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.printin(a/b)://2 System.out.println(a%b);//0 }} Output 15 5 50 2 0 |

## Java Modulo Operator

Java Modulo operator or modulus operator is used to get the remainder when we divide an integer with another integer.

The % character is the modulus operator in Java.

Its syntax is- int remainder = int % int |

The modulus operator always returns an integer. If we try to use the modulo operator with any other type of variable, we will get a compilation error.

Some examples of modulus operators with negative integer

Examples- 10 % 3 = 1 -10 % 3 = -1 10 % -3 = 1 -10%-3 = -1 |

## Java Unary Operator

The Java unary operators require only one operand.

The increment operator (–)- It is used for incrementing the value by 1. There are two varieties of increment operator which is-

- Post-Increment- Value is first used for computing the result and then incremented.
- Pre-Increment- Value is incremented first and then the result is computed. The decrement operator (–)- Is used for decrementing the value by 1. There are two varieties of decrement operators are

- Post-decrement – Value is first used for computing the result and then decremented.
- Pre-Decrement- Value is decremented first and then the result is computed.

Java ++ and — Operator Example class test{ public static void main(String args[]) { int x=10; System.out println(x++); System.out.println(++x); System.out println(x–); System.out println(–x); }} Output 10 12 12 10 |

## Relational Operator

Relational Java operators are used to checking the equality of operands. We can also use them for the comparison of two or more values.

Operator Name | Example | Description |
---|---|---|

== (equals to) | x==y | True if x equals y. otherwise false |

! (not equal to) | x!=y | True if x is not equal to y, otherwise false |

< (less than) | x<y | True if x is less than y, otherwise false |

>(greater than) | x>y | True if x is greater than y, otherwise false |

>= (greater than or equal to) | x>=y | True if x is greater than or equal to y, otherwise false |

<=(less than or equal to) | x<=y | True if x is less than or equal to y, otherwise false |

## Logical Operators

A **logical operator** is a symbol or word used to connect two or more expressions such that the value of the compound expression produced depends only on that of the original expressions and on the meaning of the operator. Common logical operators include AND, OR, and NOT.

Language | AND | OR | NOT |
---|---|---|---|

C++ | `&&` | `||` | `!` |

C# | `&&` | `||` | `!` |

Java | `&&` | `||` | `!` |

JavaScript | `&&` | `||` | `!` |

Python | `and` | `or` | `not` |

Swift | `&&` | `||` | `!` |

Operator | Description | Example |
---|---|---|

&& (logical and) | Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. | (A && B) is false |

|| (logical or) | Called Logical OR Operator. If any of the two operands A||B is true are non-zero, then the condition becomes true. | (A||B) is true |

! (logical not) | Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. | !(A&&B) is true |

Logical Operators Example class test public static void main(String args[]) { int a= 10; int b=5;OUTPUT false 10 false int c=20; System.out.println(a<b&&++a<c); System.out.println (a); System.out.println(a>b&&++a>c); System.out.println(a) |

## Bitwise Operator

Bitwise operators are used for manipulating data at the bit level, also called bit-level programming. Bit-level programming mainly consists of **0** and **1**.

Operator | Meaning |
---|---|

& | Bitwise AND operator |

| | Bitwise OR operator |

^ | Bitwise exclusive OR operator |

~ | Binary One’s Complement Operator is a unary operator |

<< | Left shift operator |

>> | Right shift operator |

>>> | Unsigned Right shift operator |

### Bitwise AND, OR and XOR

x | y | x&y | x|y | x^y |

0 | 0 | 0 | 0 | 0 |

0 | 1 | 0 | 1 | 1 |

1 | 0 | 0 | 1 | 1 |

1 | 1 | 1 | 1 | 0 |

Example a = 12, b = 25; 12 = 00001100 (In Binary) 25 = 00011001 (In Binary) |

Bitwise one’s complement operator (~)

Bitwise one’s complement operator will invert the binary bits.

If a bit is **1**, it will change it to **0**.

If the bit is **0**, it will change it to **1**.

Bitwise complement operator

0 to 1and 1 to 0

### Bitwise Shift Operators

**>> (Signed right shift)- In Java, the operator >>**‘ is signed right shift operator. All integers are signed in Java, and it is fine to use**>>**for negative numbers. The operator ‘**>>**‘ uses the sign bit (**left-most bit**) to fill the trailing positions after the shift. If the number is negative, then 1 is used as a filler and if the number is positive, then**0**is used as a filler.**>>> (Unsigned right shift)- In Java, the operator >>>**‘ is unsigned right shift operator. It always fills**0**irrespective of the sign of the number.

## Conditional Operator

The conditional operator is also known as the ternary operator This operator consists of three operands and is used to evaluate Boolean expressions The goal of the operator is to decide which value should be assigned to the variable.

result= expression1? expression2:expression3; class test{ public static void main(String [] args); String s; int a=6, b= 12 ; out= a==b ?”Yes” “No”; System.out println(“Ans”+s); } } |

Example import java utilScanner; public class test { public class test public (static void main(String[] args) { int a, b, c result Scanner inputenew Scanner(Systemin); System.out print(“Enter the Three Number:”); a= input nextint(); b= inputnextint(); c=inputnextint(); result= (a>b)? ((a>c)?ac):{(b>c)?b c); System out println( result * “is Greatest”); } } |

## Assignment Operator

The assignment operator () is the most commonly used binary operator in Java it evaluates the operand on the right-hand side and then assigns the resulting value to a variable on the left-hand side. The right operand can be a variable, constant function call, or expression The type of right operand must be type compatible with the left operand.

Example A= 3//constant X= y= 10// expression Consider a statement x=y=z 3; |

Such type of assignment statement in which a single value is given to a number of variables is called a multiple assignment statement ie value 3 is assigned to the variables x,y, and z. The assignment operator () has a right to left associatively

## Compound Assignment Operators

The compound-assignment operators combine the simple-assignment operator with another binary operator. Compound-assignment operators perform the operation specified by the additional operator, then assign the result to the left operand.

Operator | Usage | Effect |
---|---|---|

+= | a+=b; | a=a+b; |

-= | a-=b; | a=a-b; |

*= | a*=b; | a=a*b; |

/= | a/=b; | a=a/b; |

%= | a%=b; | a=a%b; |

So, that is all for today guys see you in our next blog. If you like our article please don’t forget to share with others & follow our **Instagram page** for your daily dose of Motivation.

Thank You,

Regards

Grooming Urban

## General FAQ

### What are the types of operators in Java?

Following are the types of operators in Java-**1. Arithmetic operators2. Java Modulo operator 3. Java Unary operators 4. Relational Java operators 5. Logical operators 6. Bitwise operators 7. Conditional operator8. Assignment operator 9. Compound-assignment operators **

### What is the arithmetic operator?

Arithmetic operators are used to performing simple arithmetic operations on primitive data types.

### What is the Java modulo operator?

Java Modulo operator or modulus operator is used to get the remainder when we divide an integer with another integer.

The** % **character is the modulus operator in Java. The modulus operator always returns an integer. If we try to use the modulo operator with any other type of variable, we will get a compilation error.

### What are Java unary operators?

The Java unary operators require only one operand and the increment operator (–)- It Is used for incrementing the value by 1.

### What are Relational Java operators?

Relational Java operators are used to checking the equality of operands. We can also use them for the comparison of two or more values.

### What is the logical operator?

A **logical operator** is a symbol or word used to connect two or more expressions such that the value of the compound expression produced depends only on that of the original expressions and on the meaning of the operator. Common logical operators include AND, OR, and NOT.

### What are bitwise operators?

Bitwise operators are used for manipulating data at the bit level, also called bit-level programming. Bit-level programming mainly consists of **0** and **1**.

### What is the conditional operator?

The **conditional operator** is also known as the ternary operator This operator consists of three operands and is used to evaluate Boolean expressions The goal of the operator is to decide which value should be assigned to the variable.

### What are assignment operators?

The **assignment operator** () is the most commonly used binary operator in Java it evaluates the operand on the right-hand side and then assigns the resulting value to a variable on the left-hand side. The right operand can be a variable, constant function call, or expression The type of right operand must be type compatible with the left operand.

### What are compound-assignment operators?

The compound-assignment operators combine the simple-assignment operator with another binary operator. Compound-assignment operators perform the operation specified by the additional operator, then assign the result to the left operand.

## 1 thought on “Operators in Java”