What is Operator?
Operators are symbols which take one or more operands or expressions and perform arithmetic or logical computations.
Operands are variable or expression which are used in conjunction with operators to evaluate the expression.
Combination of operands and operators from an expression.
1. Arithmetic Operator?
An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division etc on numerical values (constants and variables).
Operators are symbols which take one or more operands or expressions and perform arithmetic or logical computations.
Operands are variable or expression which are used in conjunction with operators to evaluate the expression.
Combination of operands and operators from an expression.
Types of Operators
- Arithmetic Operators
- Logical Operators
- Relational Operators
- Assignment Operators
- Conditional Operator
- Increment / Decrement Operators
- Bitwise Operators/
- Shift Operators
1. Arithmetic Operator?
An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division etc on numerical values (constants and variables).
Operator
|
Meaning of Operator
|
+
|
Addition
|
-
|
Subtraction
|
/
|
Division
|
*
|
Multiplication
|
%
|
Remainder after Division (Modulo
Division)
|
Properties of Arithmetic Operators
- These are “Binary operator” and they take two operands.
- The Operands must be “Numeric Numbers”.
- When both operands are integer, then result is integer. But when one of them is floating point and other is integer then the result is floating point.
- Except “%” operators, all the arithmetic operators can be used with any type of numeric operands (either operands are integer and floating point), while “%” operator can be used with only integer data type.
Example of Arithmetic Operators
import java.io.*;
class sum
{
public static void main (String args[])
{
int a=4,b=9,c;
c=a+b;
System.out.println(“The sum is =”+ c);
}
Output:
The sum is =13
Conditional Operators
Java language provides “?” operator as a conditional operator.
These are “ Ternary Operators” as they take three operands.
Syntax
Expression = Condition ? Expression 1 : Expression 2 ;
The statement above is equivalent to:
if (condition)
Expression 1
else
Expression 2
The operator “?” works as follows.
Expression is evaluated first. If it is nonzero(true), then the expression 1 is evaluated and became the value of expression.
If expression is false, expression 2 is evaluated and its value becomes the value of expression.
It is necessary only one is evaluated (either expression 1 or expression 2).
So it is clear that conditional operators (Ternary Operator) used as the “if..else” statement
Example Conditional Operators
Using as if .. Else..
if(total>60)
grade=‘P’;
else
grade=‘F’;
Using Conditional Operator..
total=total>60? grade=‘P’ : grade=‘F’; or total=total>60?’P’ :’F’;
Assignment Operators
An assignment operator is used for assigning a value to a variable. The most
common assignment operator is “ = “.
Example Assignment Operators
import java.io.*;
class assign
{
public static void main (String args[])
{
int a = 5, c;
c = a; // c is 5
System.out.println("c = “+ c);
c += a; // c is 10
System.out.println("c =“+ c);
c -= a; // c is 5
System.out.println("c = “+ c);
c *= a; // c is 25
System.out.println("c =“+ c);
c /= a; // c is 5
System.out.println("c =“+ c);
c %= a; // c = 0
System.out.println("c =“+ c);
}
Output:
c = 5
c = 10
c = 5
c = 25
c = 5
c = 0
Relational Operators
A relational operator checks the relationship between two operands. If the relation is true, it
returns 1; if the relation is false, it returns value 0.
Relational operators are used in decision making and loops.
Example Relational Operators
import java.io.*;
class assign
{
public static void main (String args[])
{
int a = 5, b = 5, c = 10;
System.out.println(+a+"== “+b +”is “+a==b+”\n");
System.out.println (+a+"== “+c+”is “+a==c+”\n");
System.out.println(+a“>” +b+“is”+a>b +“\n");
System.out.println (+a“>” +c+“is”+a>c +“\n");
System.out.println (+a“<” +b+“is”+a<b +“\n");
System.out.println (+a“<” +c+“is”+a<c +“\n");
System.out.println (+a“!=“ +b+“is”+a!=b +“\n");
System.out.println (+a“!=“ +c+“is”+a!=c +“\n");
System.out.println (+a“>=” +b+“is”+a>=b +“\n");
System.out.println (+a“>=” +c+“is”+a>=c +“\n"); System.out.println (+a“<=” +b+“is”+a<=b +“\n");
System.out.println (+a“<=” +c+“is”+a<=c +“\n");
}
Output:
5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1
Logical Operators
An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in Java programming.
Example Logical Operators
import java.io.*;
class logical
{
public static void main (String args[])
{
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
System.out.println("(a == b) && (c > b) is “+result+” \n”);
result = (a == b) && (c < b);
System.out.println("(a == b) && (c < b) is “+result+” \n”);
result = (a == b) || (c < b);
System.out.println("(a == b) || (c < b) is “+result+” \n”);
result = (a != b) || (c < b);
System.out.println("(a != b) || (c < b) is “+result+” \n”);
result = !(a != b);
System.out.println("!(a == b) is %d \n", result);
result = !(a == b);
System.out.println("!(a == b) is is “+result+” \n”);
}
Logical Operators
An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in Java programming.
Example Logical Operators
import java.io.*;
class logical
{
public static void main (String args[])
{
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
System.out.println("(a == b) && (c > b) is “+result+” \n”);
result = (a == b) && (c < b);
System.out.println("(a == b) && (c < b) is “+result+” \n”);
result = (a == b) || (c < b);
System.out.println("(a == b) || (c < b) is “+result+” \n”);
result = (a != b) || (c < b);
System.out.println("(a != b) || (c < b) is “+result+” \n”);
result = !(a != b);
System.out.println("!(a == b) is %d \n", result);
result = !(a == b);
System.out.println("!(a == b) is is “+result+” \n”);
}
Output:
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
Increment/Decrement Operators
Java programming has two operators increment ++ and decrement -- to change the value of an operand (constant or variable) by 1.
Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. These two operators are unary operators, meaning they only operate on a single operand.
Types of Increment/Decrement Operators
There are Two types Increment / Decrement Operator
Suppose you use ++ operator as prefix like: ++var. The value of var is incremented by 1 then, it returns the value.
Suppose you use ++ operator as postfix like: var++. The original value of var is returned first then, var is incremented by 1.
Example Increment Operators
import java.io.*;
class increment
{
public static void main (String args[])
{
int var=5;
// 5 is displayed then, var is increased to 6.
System.out.println(+ var++ +"n");
// Initially, var = 6. It is increased to 7 then, it is displayed.
System.out.println(+ ++var);
}
Output:
5
7
Example Decrement Operators
import java.io.*;
class decrement
{
public static void main (String args[])
{
int var=5;
// 5 is displayed then, var is decreased to 4.
System.out.println(+ var-- +"n");
// Initially, var = 4. It is decreased to 3 then, it is displayed.
System.out.println(+ --var);
}
Output:
5
3
Bitwise Operators
In arithmetic-logic unit (which is within the CPU), mathematical operations like: addition, subtraction, multiplication and division are done in bit-level. To perform bit-level operations in Java programming, bitwise operators are used.
Bitwise AND Operators
The output of bitwise AND is 1 if the corresponding bits of two operands is 1. If either bit of an operand is 0, the result of corresponding bit is evaluated to 0.
Let us suppose the bitwise AND operation of two integers 12 and 25.
Example Bitwise AND Operators
import java.io.*;
class increment
{
public static void main (String args[])
{
int a = 12, b = 25;
System.out.println(“Result is= “+ a&b);
}
Output:
Result is=8
Bitwise OR Operators
The output of bitwise OR is 1 if at least one corresponding bit of two operands is 1. In Java Programming, bitwise OR operator is denoted by |.
Example Bitwise OR Operators
import java.io.*;
class increment
{
public static void main (String args[])
{
int a = 12, b = 25;
System.out.println(“Result is= “+ a|b);
}
Output:
Result is=29
Bitwise XOR Operators
The result of bitwise XOR operator is 1 if the corresponding bits of two operands are opposite. It is denoted by ^.
Example Bitwise XOR Operators
import java.io.*;
class increment
{
public static void main (String args[])
{
int a = 12, b = 25;
System.out.println(“Result is= “+ a^b);
}
Output:
Result is=21
Bitwise Complement Operators
Bitwise compliment operator is an unary operator (works on only one operand). It changes 1 to 0 and 0 to 1. It is denoted by ~.
2's Complement
Two's complement is an operation on binary numbers. The 2's complement of a number is equal to the complement of that number plus 1. For example:
The bitwise complement of 35 is 220 (in decimal). The 2's complement of 220 is -36. Hence, the output is -36 instead of 220.
Bitwise complement of any number N is -(N+1). Here's how:
Twist in bitwise complement operator in Java Programming
The bitwise complement of 35 (~35) is -36 instead of 220, but why?
For any integer n, bitwise complement of n will be -(n+1). To understand this, you should have the knowledge of 2's complement.
Example Bitwise Complement Operators
import java.io.*;
class increment
{
public static void main (String args[])
{
System.out.println(“Result is= “+~35 +”\n”);
System.out.println("Output = “+ ~-12 +”\n”);
}
Output:
Result is =-36
Result is =11
Shift Operators
There are two shift operators in Java programming:
Right Shift Operators
Right shift operator shifts all bits towards right by certain number of specified bits. It is denoted by >>.
Left Shift Operators
Left shift operator shifts all bits towards left by certain number of specified bits. It is denoted by <<.
Example ShiftOperators
import java.io.*;
class shift
{
public static void main (String args[])
{
int num=212, i;
for (i=0; i<=2; ++i)
System.out.println("Right shift by “+ i+”:”+num>>i +”\n”);
System.out.println("\n");
for (i=0; i<=2; ++i)
System.out.println("Left shift by “+ i+”:”+num<<i +”\n”);
}
Output :
Right Shift by 0: 212
Right Shift by 1: 106
Right Shift by 2: 53
Left Shift by 0: 212
Left Shift by 1: 424
Left Shift by 2: 848
import java.io.*;
class sum
{
public static void main (String args[])
{
int a=4,b=9,c;
c=a+b;
System.out.println(“The sum is =”+ c);
}
Output:
The sum is =13
Conditional Operators
Java language provides “?” operator as a conditional operator.
These are “ Ternary Operators” as they take three operands.
Syntax
Expression = Condition ? Expression 1 : Expression 2 ;
The statement above is equivalent to:
if (condition)
Expression 1
else
Expression 2
The operator “?” works as follows.
Expression is evaluated first. If it is nonzero(true), then the expression 1 is evaluated and became the value of expression.
If expression is false, expression 2 is evaluated and its value becomes the value of expression.
It is necessary only one is evaluated (either expression 1 or expression 2).
So it is clear that conditional operators (Ternary Operator) used as the “if..else” statement
Example Conditional Operators
Using as if .. Else..
if(total>60)
grade=‘P’;
else
grade=‘F’;
Using Conditional Operator..
total=total>60? grade=‘P’ : grade=‘F’; or total=total>60?’P’ :’F’;
Assignment Operators
An assignment operator is used for assigning a value to a variable. The most
common assignment operator is “ = “.
Operator
|
Example
|
Same as
|
=
|
a = b
|
a = b
|
+=
|
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
|
Example Assignment Operators
import java.io.*;
class assign
{
public static void main (String args[])
{
int a = 5, c;
c = a; // c is 5
System.out.println("c = “+ c);
c += a; // c is 10
System.out.println("c =“+ c);
c -= a; // c is 5
System.out.println("c = “+ c);
c *= a; // c is 25
System.out.println("c =“+ c);
c /= a; // c is 5
System.out.println("c =“+ c);
c %= a; // c = 0
System.out.println("c =“+ c);
}
Output:
c = 5
c = 10
c = 5
c = 25
c = 5
c = 0
Relational Operators
A relational operator checks the relationship between two operands. If the relation is true, it
returns 1; if the relation is false, it returns value 0.
Relational operators are used in decision making and loops.
Operator
|
Meaning of Operator
|
Example
|
==
|
Equal to
|
5 == 3 is evaluated to 0
|
>
|
Greater than
|
5 > 3 is evaluated to 1
|
<
|
Less than
|
5 < 3 is evaluated to 0
|
!=
|
Not equal to
|
5 != 3 is evaluated to 1
|
>=
|
Greater than or equal to
|
5 >= 3 is evaluated to 1
|
<=
|
Less than or equal to
|
5 <= 3 is evaluated to 0
|
Example Relational Operators
import java.io.*;
class assign
{
public static void main (String args[])
{
int a = 5, b = 5, c = 10;
System.out.println(+a+"== “+b +”is “+a==b+”\n");
System.out.println (+a+"== “+c+”is “+a==c+”\n");
System.out.println(+a“>” +b+“is”+a>b +“\n");
System.out.println (+a“>” +c+“is”+a>c +“\n");
System.out.println (+a“<” +b+“is”+a<b +“\n");
System.out.println (+a“<” +c+“is”+a<c +“\n");
System.out.println (+a“!=“ +b+“is”+a!=b +“\n");
System.out.println (+a“!=“ +c+“is”+a!=c +“\n");
System.out.println (+a“>=” +b+“is”+a>=b +“\n");
System.out.println (+a“>=” +c+“is”+a>=c +“\n"); System.out.println (+a“<=” +b+“is”+a<=b +“\n");
System.out.println (+a“<=” +c+“is”+a<=c +“\n");
}
Output:
5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1
Logical Operators
An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in Java programming.
Example Logical Operators
import java.io.*;
class logical
{
public static void main (String args[])
{
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
System.out.println("(a == b) && (c > b) is “+result+” \n”);
result = (a == b) && (c < b);
System.out.println("(a == b) && (c < b) is “+result+” \n”);
result = (a == b) || (c < b);
System.out.println("(a == b) || (c < b) is “+result+” \n”);
result = (a != b) || (c < b);
System.out.println("(a != b) || (c < b) is “+result+” \n”);
result = !(a != b);
System.out.println("!(a == b) is %d \n", result);
result = !(a == b);
System.out.println("!(a == b) is is “+result+” \n”);
}
Logical Operators
An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in Java programming.
Operator
|
Meaning
|
Example
|
&&
|
Logical AND. True only if all
operands are true
|
If c = 5 and d = 2 then,
expression ((c==5) && (d>5)) equals to 0.
|
||
|
Logical OR. True only if either
one operand is true
|
If c = 5 and d = 2 then,
expression ((c==5) || (d>5)) equals to 1.
|
!
|
Logical NOT. True only if the
operand is 0
|
If c = 5 then, expression !(c==5)
equals to 0.
|
Example Logical Operators
import java.io.*;
class logical
{
public static void main (String args[])
{
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
System.out.println("(a == b) && (c > b) is “+result+” \n”);
result = (a == b) && (c < b);
System.out.println("(a == b) && (c < b) is “+result+” \n”);
result = (a == b) || (c < b);
System.out.println("(a == b) || (c < b) is “+result+” \n”);
result = (a != b) || (c < b);
System.out.println("(a != b) || (c < b) is “+result+” \n”);
result = !(a != b);
System.out.println("!(a == b) is %d \n", result);
result = !(a == b);
System.out.println("!(a == b) is is “+result+” \n”);
}
Output:
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
Increment/Decrement Operators
Java programming has two operators increment ++ and decrement -- to change the value of an operand (constant or variable) by 1.
Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. These two operators are unary operators, meaning they only operate on a single operand.
Types of Increment/Decrement Operators
There are Two types Increment / Decrement Operator
- Pre (Increment/Decrement)
- Post (Increment/Decrement)
Suppose you use ++ operator as prefix like: ++var. The value of var is incremented by 1 then, it returns the value.
Suppose you use ++ operator as postfix like: var++. The original value of var is returned first then, var is incremented by 1.
Example Increment Operators
import java.io.*;
class increment
{
public static void main (String args[])
{
int var=5;
// 5 is displayed then, var is increased to 6.
System.out.println(+ var++ +"n");
// Initially, var = 6. It is increased to 7 then, it is displayed.
System.out.println(+ ++var);
}
Output:
5
7
Example Decrement Operators
import java.io.*;
class decrement
{
public static void main (String args[])
{
int var=5;
// 5 is displayed then, var is decreased to 4.
System.out.println(+ var-- +"n");
// Initially, var = 4. It is decreased to 3 then, it is displayed.
System.out.println(+ --var);
}
Output:
5
3
Bitwise Operators
In arithmetic-logic unit (which is within the CPU), mathematical operations like: addition, subtraction, multiplication and division are done in bit-level. To perform bit-level operations in Java programming, bitwise operators are used.
Operators
|
Meaning of operators
|
&
|
Bitwise AND
|
|
|
Bitwise OR
|
^
|
Bitwise XOR
|
~
|
Bitwise complement
|
<<
|
Shift left
|
>>
|
Shift right
|
Bitwise AND Operators
The output of bitwise AND is 1 if the corresponding bits of two operands is 1. If either bit of an operand is 0, the result of corresponding bit is evaluated to 0.
Let us suppose the bitwise AND operation of two integers 12 and 25.
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bit Operation of 12 and 25
00001100
& 00011001
________
00001000 = 8 (In decimal)
Example Bitwise AND Operators
import java.io.*;
class increment
{
public static void main (String args[])
{
int a = 12, b = 25;
System.out.println(“Result is= “+ a&b);
}
Output:
Result is=8
Bitwise OR Operators
The output of bitwise OR is 1 if at least one corresponding bit of two operands is 1. In Java Programming, bitwise OR operator is denoted by |.
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bitwise OR Operation of 12 and 25
00001100
| 00011001
________
00011101 = 29 (In decimal)
import java.io.*;
class increment
{
public static void main (String args[])
{
int a = 12, b = 25;
System.out.println(“Result is= “+ a|b);
}
Output:
Result is=29
Bitwise XOR Operators
The result of bitwise XOR operator is 1 if the corresponding bits of two operands are opposite. It is denoted by ^.
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bitwise XOR Operation of 12 and 25
00001100
^ 00011001
________
00010101 = 21 (In decimal)
Example Bitwise XOR Operators
import java.io.*;
class increment
{
public static void main (String args[])
{
int a = 12, b = 25;
System.out.println(“Result is= “+ a^b);
}
Output:
Result is=21
Bitwise Complement Operators
Bitwise compliment operator is an unary operator (works on only one operand). It changes 1 to 0 and 0 to 1. It is denoted by ~.
35 = 00100011 (In Binary)
Bitwise complement Operation of 35
~ 00100011
________
11011100 = 220 (In decimal)
2's Complement
Two's complement is an operation on binary numbers. The 2's complement of a number is equal to the complement of that number plus 1. For example:
Decimal
Binary
2's complement
0 00000000 -(11111111+1) =
-00000000 = -0(decimal)
1 00000001 -(11111110+1) =
-11111111 = -256(decimal)
12 00001100 -(11110011+1) =
-11110100 = -244(decimal)
220 11011100 -(00100011+1) =
-00100100 = -36(decimal)
Note: Overflow is ignored while computing 2's complement.
The bitwise complement of 35 is 220 (in decimal). The 2's complement of 220 is -36. Hence, the output is -36 instead of 220.
Bitwise complement of any number N is -(N+1). Here's how:
bitwise complement of N = ~N (represented in 2's complement form)
2'complement of ~N= -(~(~N)+1) = -(N+1)
Twist in bitwise complement operator in Java Programming
The bitwise complement of 35 (~35) is -36 instead of 220, but why?
For any integer n, bitwise complement of n will be -(n+1). To understand this, you should have the knowledge of 2's complement.
Example Bitwise Complement Operators
import java.io.*;
class increment
{
public static void main (String args[])
{
System.out.println(“Result is= “+~35 +”\n”);
System.out.println("Output = “+ ~-12 +”\n”);
}
Output:
Result is =-36
Result is =11
Shift Operators
There are two shift operators in Java programming:
- Right shift operator
- Left shift operator.
Right Shift Operators
Right shift operator shifts all bits towards right by certain number of specified bits. It is denoted by >>.
212 = 11010100 (In binary)
212>>2 = 00110101 (In binary) [Right shift by two bits]
212>>7 = 00000001 (In binary)
212>>8 = 00000000
212>>0 = 11010100 (No Shift)
Left Shift Operators
Left shift operator shifts all bits towards left by certain number of specified bits. It is denoted by <<.
212 = 11010100 (In binary)
212<<1 = 110101000 (In binary) [Left shift by one bit]
212<<0 =11010100 (Shift by 0)
212<<4 = 110101000000 (In binary) =3392(In decimal)
Example ShiftOperators
import java.io.*;
class shift
{
public static void main (String args[])
{
int num=212, i;
for (i=0; i<=2; ++i)
System.out.println("Right shift by “+ i+”:”+num>>i +”\n”);
System.out.println("\n");
for (i=0; i<=2; ++i)
System.out.println("Left shift by “+ i+”:”+num<<i +”\n”);
}
Output :
Right Shift by 0: 212
Right Shift by 1: 106
Right Shift by 2: 53
Left Shift by 0: 212
Left Shift by 1: 424
Left Shift by 2: 848