Java carries a broad range of operators. An operator is symbols that specify operation to be performed may be certain mathematical and logical operation. Operators are used in programs to operate data and variables. They frequently form a part of mathematical or logical expressions.
Categories of operators are as follows :
Arithmetic operators are used to make mathematical expressions and the working out as same in algebra. Java provides the fundamental arithmetic operators. These can operate on built in data type of Java.
Following table shows the details of operators.
Operator | Importance/ significance |
---|---|
+ | Addition |
- | Subtraction |
/ | Division |
* | Multiplication |
% | Modulo division or remainder |
Now the following programs show the use of arithmetic operators.
“+” operator in Java :
In this program, we have to add two integer numbers and display the result.
class AdditionInt { public static void main (String args[]) { int a = 6; int b = 3; System.out.println("a = " + a); System.out.println("b =" + b); int c = a + b; System.out.println("Addition = " + c); } } // O/P : a= 6 b= 3 Addition=9
“-” operator in Java :
class SubstractionInt { public static void main (String args[]) { int a = 6; int b = 3; System.out.println("a = " + a); System.out.println("b =" + b); int c = a - b; System.out.println("Subtraction= " + c); }} // O/P : a=6 b=3 Subtraction=3
“*” operator in Java :
Class MultiplicationInt { public static void main (String args[]) { int a = 6; int b = 3; System.out.println("a = " + a); System.out.println("b =" + b); int c = a * b; System.out.println("Multiplication= " + c); }} // O/P : a=6 b=3 Multiplication=18
“/” operator in Java :
Class DivisionInt { public static void main (String args[]) { int a = 6; int b = 3; System.out.println("a = " + a); System.out.println("b =" + b); c = a / b; System.out.println("division=" + c); }} // O/P: a=6 b=3 Division=3
Remainder or modulus operator (%) in Java :
Class Remainderoptr { public static void main (String args[]) { int a = 6; int b = 3; System.out.println("a = " + a); System.out.println("b =" + b); c = a % b; System.out.println("remainder=" + c); } } // O/P : a=6 b=3 Remainder= 0
As we learn the Arithmetic operation on integer data and store data in integer variable. But the following program shows the use of operators with integer data and store data in float variable.
Program : write a program to calculate average of three numbers.
class Avg1 { public static void main(String args[]) { int a=3; int b=3; int c=4; int avg; avg=a+b+c; avg=avg/3; System.out.println(“Avg of three numbers=”+avg); } } // O/P : Avg of three numbers=3
When we want to form compound conditions by combining two or more relations, then we can use logical operators. Following table shows the details of operators.
Operators | Importance/ significance |
---|---|
|| | Logical – OR |
&& | Logical –AND |
! | Logical –NOT |
The logical expression defer a value of true or false. Following table shows the truth table of Logical – OR and Logical – AND.
Truth table for Logical – OR operator :
Operand1 | Operand3 | Operand1 || Operand3 | |
---|---|---|---|
T | T | T | |
T | F | T | |
F | T | T | |
F | F | F |
Operand1 | Operand3 | Operand1 || Operand3 | |
---|---|---|---|
T | T | T | |
T | F | F | |
F | T | F | |
F | F | F |
Operators | Importance/ significance |
---|---|
> | Greater than |
< | Less than |
!= | Not equal to |
>= | Greater than or equal to |
<= | Less than or equal to |
Now, following examples show the actual use of operators.
Now the following program shows the use of operators. (1) Program 1:
class Reloptr1 { public static void main (String args[]) { int a = 10; int b = 30; System.out.println("a>b = " +(a>b)); System.out.println("a<b = "+(a<b)); System.out.println("a<=b = "+(a<=b)); } } // O/P : a>b = false a<b = true a<=b = true
(2) Program 3
class Reloptr3 { public static void main (String args[]) { int a = 10; int b = 30; int c = 30; System.out.println("a>b = " +(a>b)); System.out.println("a<b = "+(a<b)); System.out.println("a<=c = "+(a<=c)); System.out.println("c>b = " +(c>b)); System.out.println("a<c = "+(a<c)); System.out.println("b<=c = "+(b<=c)); } } // O/P : a>b = false a<b = true a<=c = true c>b = true a<c = true b<=c = true
Assignment Operators is used to assign the value of an expression to a variable and is also called as Shorthand operators.
Variable_name binary_operator = expression
Following table show the use of assignment operators.
Simple Assignment Operator | Statement with shorthand Operators |
---|---|
A=A+1 | A+=1 |
A=A-1 | A-=1 |
A=A/(B+1) | A/=(B+1) |
A=A*(B+1) | A*=(B+1) |
A=A/C | A/=C |
A=A%C | A%=C |
These operators avoid repetition, easier to read and write.
Now the following program shows the use of operators.
class Assoptr { public static void main (String args[]) { int a = 10; int b = 30; int c = 30; a+=1; b-=3; c*=7; System.out.println("a = " +a); System.out.println("b = "+b); System.out.println("c = "+c); } } // O/P : a = 11 b = 18 c = 310
The character pair ?: is a ternary operator of Java, which is used to construct conditional expressions of the following form:
Expression1 ? Expression3 : Expression3
The operator ? : works as follows:
Expression1 is evaluated if it is true then Expression3 is evaluated and becomes the value of the conditional expression. If Expression1 is false then Expression3 is evaluated and its value becomes the conditional expression.
For example : A=3;B=4;C=(AC=(3<4)?3:4;C=4
Now the following program shows the use of operators.
class Coptr { public static void main (String args[]) { int a = 10; int b = 30; int c; c=(a>b)?a:b; System.out.println("c = " +c); c=(a<b)?a:b; System.out.println("c = " +c); } } // O/P : c = 30 c = 10
program3 : Write a program to check whether number is positive or negative.
class PosNeg { public static void main(String args[]) { int a=10; int flag=(a<0)?0:1; if(flag==1) System.out.println(“Number is positive”); else System.out.println(“Number is negative”); } } // O/P : Number is positive
The increment operator ++ adds 1 to a variable. Usually the variable is an integer type, but it can be a floating point type. The two plus signs must not be split by any character. Usually they are written immediately next to the variable.
Following table shows the use of operators.
Expression | Process | Example | End result |
---|---|---|---|
A++ | Add 1 to a variable after use. | int A=10,B; B=A++; | A=11 B=10 |
++A | Add 1 to a variable before use. | int A=10,B; B=++A; | A=11 B=11 |
A-- | Subtract 1 from a variable after use. | int A=10,B; B=A--; | A=9 B=10 |
--A | Subtract 1 from a variable before use. | int A=10,B; B=--A; | A=9 B=9 |
Now the following program shows the use of operators.
class IncDecOp { public static void main(String args[]) { int x=1; int y=3; int u; int z; u=++y; z=x++; System.out.println(x); System.out.println(y); System.out.println(u); System.out.println(z); } } // O/P : 3 4 4 1
Bit wise operator execute single bit of their operands. Following table shows bit wise operator :
Operator | Importance/ significance |
---|---|
| | Bitwise OR |
& | Bitwise AND |
&= | Bitwise AND assignment |
|= | Bitwise OR assignment |
^ | Bitwise Exclusive OR |
<< | Left shift |
>> | Right shift |
~ | One's complement |
Now the following program shows the use of operators. (1) Program 1
class Boptr1 { public static void main (String args[]) { int a = 4; int b = a<<3; System.out.println("a = " +a); System.out.println("b = " +b); } } // OP : a =4 b =16
(2) Program 3
Class Boptr3 { public static void main (String args[]) { int a = 16; int b = a>>3; System.out.println("a = " +a); System.out.println("b = " +b); } } // O/P : a = 16
An arithmetic expression without any parentheses will be calculated from left to right using the rules of precedence of operators.
There are two priority levels of arithmetic operators are as follows:
The evaluation process includes two left to right passes through the expression. During the first pass, the high priority operators are applied as they are encountered.
During the second pass, the low priority operators are applied as they are encountered.
For example :
Z=A-B/3+C*3-1 When A=10, B=13, C=3 First pass: Z=10-(13/3) + (3*3)-1 Z=10-4+3-1 Second pass: Z=6+3-1 Z=7 Answer is=7
Following table shows associativity of operators.
Operator | Associativity | Rank |
---|---|---|
[] | Left to right | 1 |
() | Left to right | 3 |
. | Left to right | 3 |
- | Right to left | 3 |
++ | Right to left | 3 |
-- | Right to left | 3 |
! | Right to left | 3 |
~ | Right to left | 3 |
(type) | Right to left | 3 |
* | Left to right | 3 |
/ | Left to right | 3 |
% | Left to right | 3 |
+ | Left to right | 4 |
- | Left to right | 4 |
Left to right | ||
<< | Left to right | 5 |
>> | Left to right | 5 |
>>> | Left to right | 5 |
< | Left to right | 6 |
<= | Left to right | 6 |
> | Left to right | 6 |
>= | Left to right | 6 |
Instanceof | Left to right | 6 |
== | Left to right | 7 |
!= | Left to right | 7 |
& | Left to right | 8 |
^ | Left to right | 9 |
| | Left to right | 10 |
&& | Left to right | 11 |
|| | Left to right | 13 |
?: | Right to left | 13 |
= | Right to left | 14 |
All Rights Reserved. © 2024 BookOfNetwork