Operators are symbols that are used to perform operations on operands(Variables or values).
Types of Operators
Assignment Operator represented as "=" symbol.
Assignment variable will be used to assign values to the variables.
Assignment operator assigns the value to the left side variable right side value.
Ex:
Above value 10 is assigned to x variable which is on the left. so we can say Assign Operator Assigns the value to the variable which is on the left side.
Example
Assignment operator can also use to assign object references also.
Ex:
Arithmetic operators perform mathematical operations like addition, subtraction, multiplication, and division.
Example
Unary Operators
The unary operators require only one operand; Unary Operators performs operations like as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean on one operand.
Example
In unary operators we have, Increment/decrement operators they can be applied to before(prefix) or after(postfix) the operand. prefix(++x)evaluates to the incremented value. postfix(x++)evaluates to the original value.
Example
Equality and Relational Operators
The equality and relational operators will compare two operands like if one operand is greater than, less than, equal to, or not equal to another operand.The result will be a boolean value like true or false.
Example
Conditional operators
Conditional Operators can be applied to boolean expressions.
Example
Ternary(?:) operator
Ternary(?:) operator is shorthand of if-then-else statement.
synatx:
Output = (condition)?value1(if condition true):value 2(if condition false)
If condition is true value1 is executed and assigns to output. if condition is false value 2 is exexuted and assigns to output.
Example
Instanceof operator
The instanceof operator compares an object to a specified type.
Ex:
we can test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.
Example
Bitwise operators
Bitwise operators that perform bitwise and bit shift operations on integral types.
Types of Operators
- Assignment operators
- Arithmetic operators
- Unary operators
- Equality operators
- Relational operators
- Conditional operators
- Bitwise operators
Assignment Operator represented as "=" symbol.
Assignment variable will be used to assign values to the variables.
Assignment operator assigns the value to the left side variable right side value.
Ex:
int x = 10;
Above value 10 is assigned to x variable which is on the left. so we can say Assign Operator Assigns the value to the variable which is on the left side.
Example
public class Assignment_Operator_Demo { public static void main(String[] args) { int x;// variable declaration int y;// variable declaration x = 150;// assign value to variable y = 200;// assign value to the variable System.out.println("x variable value is :" + x); System.out.println("y variable value is :" + y); } }output
Assignment operator can also use to assign object references also.
Ex:
Test t = new Test();
Arithmetic OperatorsArithmetic operators perform mathematical operations like addition, subtraction, multiplication, and division.
| Operator | Description |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Divison |
| % | Remainder |
public class Arithmetic_Operators_Demo { public static void main(String[] args) { int x = 10; int y = 20; int addition; int subtraction; int multiplication; int divison; int reminder; // addition addition = x + y; System.out.println("addition of numbers is :" + addition); // subtraction subtraction = x - y; System.out.println("subtraction of numbers is :" + subtraction); // multiplication multiplication = x * y; System.out.println("multiplication of numbers is :" + multiplication); // divison divison = x / y; System.out.println("division of numbers is :" + divison); // reminder reminder = x % y; System.out.println("reminder of numbers is :" + reminder); } }Output
Unary Operators
The unary operators require only one operand; Unary Operators performs operations like as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean on one operand.
| Operator | Description |
|---|---|
| + | Unary plus operator; |
| - | Unary minus operator; negates an expression |
| ++ | Increment operator; increments a value by 1 |
| -- | Decrement operator; decrements a value by 1 |
| ! | Logical complement operator; inverts the value of a boolean |
public class Unary_Demo { public static void main(String[] args) { int x = 15; boolean b = true; System.out.println(+x);// 15 System.out.println(-x);// -15 System.out.println(++x);// 16 System.out.println(--x);// 15 System.out.println(b);// true System.out.println(!b);// false } }output
In unary operators we have, Increment/decrement operators they can be applied to before(prefix) or after(postfix) the operand. prefix(++x)evaluates to the incremented value. postfix(x++)evaluates to the original value.
Example
public class Unary_demo1 { public static void main(String[] args) { int x = 15;// System.out.println(x++);// 15; int y = 20; System.out.println(++y);// 21 int a = 6; System.out.println(a--);// 6 int b = 7; System.out.println(--b);// 6 } }output
Equality and Relational Operators
The equality and relational operators will compare two operands like if one operand is greater than, less than, equal to, or not equal to another operand.The result will be a boolean value like true or false.
| Operator | Description | Example | Output |
|---|---|---|---|
| == | equal to | 30==40 | false |
| != | not equal to | 30!=40 | true |
| > | greater than | 30>40 | false |
| >= | greater than or equal to | 30>40 | false |
| < | less than | 30<40 td="">40> | true |
| <= | less than or equal to | 30<=40 | true |
public class Relational_Operator_Demo { public static void main(String[] args) { int x = 30; int y = 40; System.out.println("30==40 :" + (x == y));// false System.out.println("30!=40 :" + (x != y));// true System.out.println("30>40 :" + (x > y));// false System.out.println("30>=40 :" + (x >= y));// false System.out.println("30<=40 :" + (x <= y));Output
Conditional operators
Conditional Operators can be applied to boolean expressions.
| Operator | Description |
|---|---|
| ||(Conditional-OR) | ||--returns true if atleast one boolean expression true otherwise it returns false. |
| &&(Conditional-AND) | &&-returns true if both boolean expressions are true otherwise it returns false. |
public class Logical_Operator_Demo { public static void main(String[] args) { int x = 30; int y = 40; boolean output = true; if ((x > 20) && (y < 50)) { System.out.println(output); } if ((x < 20) || (y < 50)) { System.out.println(output); } } }Output
Ternary(?:) operator
Ternary(?:) operator is shorthand of if-then-else statement.
synatx:
Output = (condition)?value1(if condition true):value 2(if condition false)
If condition is true value1 is executed and assigns to output. if condition is false value 2 is exexuted and assigns to output.
Example
public class Ternary_Operator { public static void main(String[] args) { int x = 30; int y = 40; int output = (x < y) ? x : y; System.out.println(output);// 30 int output1 = (x > y) ? x : y; System.out.println(output1);// 40 } }Output
Instanceof operator
The instanceof operator compares an object to a specified type.
Ex:
we can test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.
Example
public class InstanceOf_Demo { public static void main(String[] args) { String message = "welcome"; System.out.println(message instanceof String); System.out.println(message instanceof Object); } }Output
Bitwise operators
Bitwise operators that perform bitwise and bit shift operations on integral types.
| Operator | Description |
|---|---|
| & | bitwise AND operation |
| | | inclusive OR operation |
| ^ | bitwise exclusive OR operation |