**Operator In Java:** When we are writing Java Programming, then our main moto of that is to do some operations using that program. For example, if in your program, if you want to add two numbers, then you are going to use + (addition) symbol by using that you can add two numbers. That means this + symbol performs an operation that is the addition. Such symbols are called operators, and by using that, we can make our programming easy.

## What is the Operator or Operator In Java?

An operator is a symbol that performs some operation. An operator acts on some variables called operands to get the desired result. According to the implementation, there are different types of the operator are there like:

**Unary Operator:**If an operator acts on a single variable, then that is called a unary operator.**Binary Operator:**If an operator acts on two variables, then that is called a binary operator.**Ternary Operator:**If an operator acts on 3 variables then, that is called a ternary operator.

Also, in Java programming language, there are different types of operators are there according to the classification. Below we have mentioned the different types of operators based on their classification:

## Arithmetic Operator

These operators are used to perform fundamental arithmetic operations like addition, subtraction, etc. on the primitive data types. There are five arithmetic operators in java, and here is the list:

- *: Multiplication
- /: Division
- %: Modulo
- +: Addition
- –: Subtraction

Since these operators act upon two operands at a time, that’s why these are called binary operators.

Example Program:

public class Operators { public static void main(String[] args) { // TODO Auto-generated method stub int a = 11, b = 22; String string1 = "Software", string2 = "Testingo"; // + and - operator System.out.println("a + b = " + (a + b)); System.out.println("a - b = " + (a - b)); // + operator if used with strings System.out.println(" USe of + Operator With String " + string1 + string2); // * and / operator System.out.println("a * b = " + (a * b)); System.out.println("a / b = " + (a / b)); // modulo operator gives remainder // on dividing first operand with second System.out.println("a % b = " + (a % b)); } }

### Unary Operator In Java

As the name indicated unary operators act on only one operand. These operators are mainly used for the increment, decrement, or negative value to primitive data type variables.

**– :** **Unary minus** is used when you want to assign a negative value.

**+:** **Unary plus**, This is used when you want to convert a negative value to positive value.

**+ +:** **Increment operator**, You can use this operator when you want to increment the value by 1. There are two varieties of increment operator, those are:

**Post-Increment:**In this case, value is first used for the operator, and after that, the value is updated by 1.**Pre-Increment:**In this case, the value is first incremented by 1 and then its use for computing.

**—** **Decrement operator**: This is act same as the increment operator, but where the value is decrement by 1. it also has two varieties, that are:

**Post-decrement:**Current value is used for computing, and later, the value is decrement by 1.**Pre-Decrement:**Here First value is decrement by one later compute is done.

**!:** **Logical not operator**: This is used to invert the boolean type value. means if the current value is true, then the operator inverts that value to false and vice versa.

Example Program:

public class UnaryOperatorsEx { public static void main(String[] args) { int a = 11, b = 22, c = 33, d = 44; boolean condition = true; System.out.println(" Current Value Of A = " + a); System.out.println(" Current Value Of B = " + b); System.out.println(" Current Value Of C = " + c); System.out.println(" Current Value Of D = " + d); // pre-increment operator System.out.println("Value of A after Pre Increment = " + (++a)); System.out.println(" Current Value Of A = " + a); // post increment operator System.out.println("Value of B After Post Increment = " + (b++)); System.out.println(" Current Value Of B = " + b); // pre-decrement operator System.out.println("Value of C Pre Decrement = " + (--c)); System.out.println("Current Value of C = " + c); // post-decrement operator System.out.println("Value of D after post decrement = " + (d--)); System.out.println("Current Value of D = " + d); // Logical not operator System.out.println("Value of !condition ="+ !condition); } }

### Relational Operator In Java

These operators are used to compare two variables. For example, to know which is one is biggers or whether two quantities are equal or not. There are six relational operators are available:

**==, Equal to**:- returns true if both the variable have the same value.**!= Not Equal to**– returns true if both the variable doesn’t have the same value.**<, less than**:- returns true of the left-hand side is less than the right-hand side.**<=, less than or equal t**o- Returns true of the left-hand side is less than or equal to the right-hand side.**> Greater than**: returns true of the left-hand side is higher than the right-hand side.**>=, Greater than or equal to**– returns true of the left-hand side is greater than or equal to the right-hand side.

Example Program:

public class RelationalOperatorsEx { public static void main(String[] args) { int a = 20, b = 10; String x = "SoftwareTestingo", y = "SoftwareTestingo"; int array1[] = { 1, 2, 3 }; int array2[] = { 1, 2, 3 }; boolean condition = true; // various conditional operators System.out.println("a == b :" + (a == b)); System.out.println("a < b :" + (a < b)); System.out.println("a <= b :" + (a <= b)); System.out.println("a > b :" + (a > b)); System.out.println("a >= b :" + (a >= b)); System.out.println("a != b :" + (a != b)); // Arrays cannot be compared with // relational operators because objects // store references not the value System.out.println("x == y : " + (array1 == array2)); System.out.println("condition==true :"+ (condition == true)); } }

Output:

a == b :false a < b :false a <= b :false a > b :true a >= b :true a != b :true x == y : false condition==true :true

### Logical Operator In Java

Logical operators are used to constructing compound conditions. a compound condition is a combination of several simple conditions. Logical operators are of 3 types:

- && and operator
- || or operator
- ! not operator

Example Program:

import java.util.Scanner; public class LogicalOperatorsEx { public static void main(String[] args) { String x = "software"; String y = "testingo"; Scanner s = new Scanner(System.in); System.out.print("Enter username:"); String uuid = s.next(); System.out.print("Enter password:"); String upwd = s.next(); // Check if user-name and password match or not. if ((uuid.equals(x) && upwd.equals(y)) || (uuid.equals(y) && upwd.equals(x))) { System.out.println("Welcome user."); } else { System.out.println("Wrong uid or password"); } } }

Output:

Enter username:software Enter password:testingo Welcome user.

### Boolean Operator In Java

These operators act on the boolean variables and produce boolean type results. there are three boolean operators are there

- & boolean and operator
- | boolean or operator
- ! boolean not operator

Boolean & operator returns true if both the values of the variables are true. The boolean | operator returns true if any one of the variables is true. Boolean! The operator converts true to false and vice versa.

## Bitwise operator In Java

There are seven types of bitwise operators. let’s discuss one by one

**Bitwise Complement Operator (~):**This operator gives the complement form of a given number. This operator symbol is ~, which is pronounced as a tilde.**Bitwise and operator (&):**This operator operates on the individual bits of the numbers. the symbol for this operator is &, which is called an ampersand.**Bitwise Or operator (|):**This operator performs or operation on the bits of the numbers. The symbol is |, which is called a pipe symbol.**Bitwise Xor operator (^):**This operator performs exclusive or (Xor) operation on the bits of the numbers. the symbol is ^, which is called a cap, carat, or circumflex symbol.**Bitwise left shift operator(<<):**This operator perform shifts the bits of the number towards left a specified number of positions. The symbol for this operator is <<, read as double less than. if we write x<<n, the meaning is to shift the bits of x towards left n positions.**Bitwise right shift operator (>>):**This operator shifts the bits of the number towards right a specified number of positions. The symbol for this operator is >>, read as double greater than. If we write X>>n, the meaning is to shift the bits of x towards right n positions.**Bitwise Zero-fill right shift operator(>>>):**This operator also shifts the bits of the number towards right a specified number of positions. But it stores 0 in the sign bit. the symbol of this operator is >>>, read as triple greater than. Since it always fills 0 in the sign bit, it is called zero-fill right shift operator. If we apply >>> on a positive number it gives the same output as that of >>. But in case of negative numbers, the output will be positive, since the sign bit is replaced by a 0.

**Ternary operator Java or conditional operator (? :):**

This operator called ternary because it acts on three variables. The other name of this operator is a conditional operator since it represents a conditional statement. Two symbols used for this operator? and.

Variable = expressions1 ? expressions2 expressions3;

This means that first of all, expressions1 evaluated. If it is true, then the expressions2 value is stored in the variable. If expressions1 is false, then the expressions3 value is stored into the variable.

**Member Operator (.):** Member operator is also called as dot operator since its symbol is a. (dot or period). This operator tells about a member of a package or a class. It used in three ways.

We know that a package contains classes. We can use it — the operator to refer to the class of a package.

**Syntax:**

Packagename.classname

We know that each class contains variables and methods. To refer to the variable of a class, we can use this operator.

**Syntax:**

Classname.variablename; or object name.variablename;

We know that a class contains methods. Using the dot operator, we can refer to the methods of a class.

**Syntax:**

classname.methodname; or objectname.methodname;

### The instance of Operator In Java

This operator is used to test if an object belongs to the class or not. Note that the word instance means object. This operator can also be used to check if an object belongs to an interface or not.

**Syntax:**

boolean variable = object instanceof class; boolean variable = object instanceof interface;

If the object is an instanceof of class or interface, then it returns true. Otherwise, it returns false.

**New Operator:** New operator is often used to create objects to classes. We know that objects are created on heap memory by JVM dynamically (at runtime).

Syntax:

classname obj= new classname();

**Cast Operator:**

Cast operators used to convert one data type to another data type. This operator can be used by writing datatype inside simple braces.

### The priority of Operator In Java

When several operators used in a statement, it is important to know which operator will execute first and which will come next. To determine that certain rules of operator precedence followed:

- First, the contents inside the braces: () and [] will be executed
- Next, ++ and —
- Next, *,/ and % will execute
- + and – will come next
- Relational operator and executed next
- Boolean and bitwise operators
- Logical operators will come afterward
- Then ternary operator
- Assignment operators are executed at the last

If you find anything wrong or you want to share more information on this topic, then you can write to us in the comment section.

**What is operator and its types?**

Operators are special type of functions, that takes one or more arguments and produces a new value. For example : addition (+), substraction (-), multiplication (*) etc, are all operators.

**What are the four operators?**

There are four different types of calculation operators: arithmetic, comparison, text concatenation (combining text), and reference.

**What are the different types of arithmetic operators?**

These operators are + (addition), - (subtraction), * (multiplication), / (division), and % (modulo). The following table summarizes the binary arithmetic operations in the Java programming language.

**What are the Java operators?**

Java operators are generally used to manipulate primitive data types. The Java operators are classified into eight different categories: assignment, arithmetic, relational, logical, bitwise, compound assignment, conditional and type comparison operators.

**What are arithmetic operators?**

Arithmetic operators are the symbols that represent arithmetic math operations. Examples include + (addition operator), - (subtraction operator), * (multiplication operator), and / (division operator).

**What comes first in arithmetic?**

This means that expressions within parentheses are evaluated first, then exponents, then multiplication and division, and finally addition and subtraction.

## Leave a Reply