Operators In Java
Operators
In Java, there are many types of Operators. Operators are use to perform some operations.
Java has around 30 operators such as,
- Assignment operator.
- Arithmetic operator.
- Relational operator.
- Unary operator.
- Logical operator.
- Conditional operator.
- Comparison operator.
- Bitwise operator.
- Assignment Operator.
( = ).
Assignment Operator is the very common operator, which we use to assign the value on its right to the operand on its left.
For example
- int a = 5;
- String msg = “hello java”;
- Arithmetic operators.
Arithmetic operators are used to achieve the mathematical calculations like algebra. The following table lists of all the arithmetic operators, which are,
Operators Explanation. + Addition (or concatenation) operator - Subtraction operator * Multiplication operator / Division operator % Remainder operator
Let’s see the examples, given below.
Code
- public class ArithmeticOperatorEx1 {
- public static void main(String[] args) {
- int p = 25;
- int q = 15;
- int res = p + q;
- System.out.println("p + q = " + res);
- res = p - q;
- System.out.println("p - q = " + res);
- res = p * q;
- System.out.println("p * q = " + res);
- res = p / q;
- System.out.println("p / q = " + res);
- res = p % 2;
- System.out.println("p % 3 = " + res);
- }
- }
Output
- Relational Operators
Relational operators are used to judge against the two operands and give result in a boolean form. The following table lists all the relational operators, which are given below.
Operators Explanation. == equal to != not equal to > greater than >= greater than or equal to < less than <= less than or equal to
Let’s see the examples, given below.
Code
- public class RelationalOperatorEx2 {
- public static void main(String[] args) {
- int p = 25;
- int q = 35;
- boolean res = p == q;
- System.out.println("p == q : " + res);
- res = p != q;
- System.out.println("p != q : " + res);
- res = p > q;
- System.out.println("p > q : " + res);
- res = p >= q;
- System.out.println("x >= q : " + res);
- res = p < q;
- System.out.println("p < q : " + res);
- res = p <= q;
- System.out.println("p <= q : " + res);
- }
- }
Output
- Unary Operators.
Unary Operators are engaged in a single operand. The following table lists all unary operators, which are,
Operators Explanation. + Unary plus operator; indicates positive value. - Unary minus operator; negate an expression. ++ Increment operator; increments a value by 1. -- Decrement operator; decrements a value by 1; ! Logical complement operator; return value of a boolean.
Let’s see the examples, given below.
Code
- public class UnaryOperatorEx3 {
- public static void main(String[] args) {
- int p = 20;
- int q = 40;
- int res = +p;
- System.out.println("+p = " + res);
- res = -q;
- System.out.println("-q = " + res);
- res = ++p;
- System.out.println("++p = " + res);
- res = --q;
- System.out.println("++q = " + res);
- boolean a = false;
- System.out.println(a);
- System.out.println(!a);
- }
- }
Output
- Logical Operators.
Logical operator is used to do logical operations in Java. The following table lists all the logical operators, which are,
Operators Explanation. && Logical AND operator. (when two operands are not zero then the condition give true). || Logical OR operator. (If any two operands are not zero then the condition give true). ! Logical NOT operator. It uses to reverse the logic.
Let’s see the example, given below.
Code
- public class LogicalOperatorEx4 {
- public static void main(String[] args) {
- boolean p = true;
- boolean q = false;
- boolean res = (p && q);
- System.out.println("p && q = " + res);
- res = (p || q);
- System.out.println("p || q = " + res);
- res = !(p && q);
- System.out.println("p ! q = " + res);
- }
- }
Output
- Conditional Operators.
Ternary Operator is a conditional operator in Java. Conditional operators are? and: which forms a ternary (three operands) such as
res = P ? Q: R
In other words, we can say that it is similar to if-else condition like: if P then Q else R. so, this is also referred as shorthand for an if-then-else statement. The table of conditional operator is given below.
Operators Explanation. ? : Ternary operator in form of: A ? B : C
Let’s see the example, given below.
Code
- public class TernaryOperatorEx6 {
- public static void main(String[] args) {
- int p = 10;
- int q = 20;
- int res = (p > 10) ? p : q;
- System.out.println("result 1 is: " + res);
- res = (q > 10) ? p : q;
- System.out.println("result 2 is: " + res);
- }
- }
Output
- Comparison Operators.
Instance of an operator is used as a comparison operator in Java. This operator tests that object of a class is an instance of a class or not and gives the result in a Boolean value. The table of comparison operator is given below.
Operators Explanation. Instanceof Instanceof operator checks the type of object.
Let’s see an example, given below.
Code
- public class InstanceofOperatorEx7 {
- public static void main(String[] args) {
- String name = "Hello";
- if (name instanceof String) {
- System.out.println("It is an instance of a String class");
- }
- // test for subclass of Object
- if (name instanceof Object) {
- System.out.println("It is an instance of an Object class");
- }
- }
- }
Output
- Bitwise Operator.
Bitwise operators are hardly ever used. These operators perform bit shift operations on only integral types and not other types. The following table lists of all bitwise operators, which are,
Operators Explanation. ~ unary bitwise complement; inverts a bit pattern << signed left shift >> signed right shift >>> unsigned right shift & bitwise AND ^ bitwise exclusive OR | bitwise inclusive OR
Let’s see the example, given below.
Code
- public class BitwiseOperatorEx8 {
- public static void main(String[] args) {
- int p = 10;
- int res = p >> 1;
- System.out.println("Before right shift: " + p);
- System.out.println("After right shift: " + res);
- }
- }
Output
Summary
Thus, we learnt that the operators are used to perform some operations and also learnt how to use it in Java.