Operator In Java

Operator :

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
  • Logical operators
  • Relational operators
  • Assignment operators
  • Conditional operators
  • Increment and decrement operators
  • Bit wise operators

Arithmetic operators :

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.

CODE/PROGRAM/EXAMPLE
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 :

CODE/PROGRAM/EXAMPLE
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 :

CODE/PROGRAM/EXAMPLE
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 :

CODE/PROGRAM/EXAMPLE
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 :

CODE/PROGRAM/EXAMPLE
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
  • When both operands in the expression are integers then the expression is called Integer expression and the opration is called Integer arithmetic.
  • When both operands in the expression are real then the expression is called Real expression and the opration is called Real arithmetic.
  • When one operand in the expression is integer and other is float then the expression is called Mixed Mode Arithmetic expression and the opration is called Mixed Mode Arithmetic operation.

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.

CODE/PROGRAM/EXAMPLE
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

Logical operators :

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 :

T - True, F - False

Truth table for Logical – AND operator :

Operand1 Operand3 Operand1 || Operand3
T T T
T F T
F T T
F F F

T - True, F - False

Now the following program shows the use of Logical operators.

CODE/PROGRAM/EXAMPLE
class LogicalOptr
{
public static void main (String args[])
{
boolean a = true;
boolean b = false;
System.out.println("a||b = " +(a||b));
System.out.println("a&&b = "+(a&&b));
System.out.println("a! = "+(!a));
}
}

// O/P : a||b = true
a&&b = false
a! = false

Relational Operators :

When evaluation of two numbers is performed depending upon their relation, assured decisions are made. The value of relational expression is either true or false.

If A=7 and A < 10 is true while 10 < A is false.

Following table shows the details of operators.

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.

  • If 10 > 30 then result is false
  • If 40 > 17 then result is true
  • If 10 >= 300 then result is false
  • If 10 <= 10 then result is true

Now the following program shows the use of operators. (1) Program 1:

CODE/PROGRAM/EXAMPLE
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

CODE/PROGRAM/EXAMPLE
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 :

Assignment Operators is used to assign the value of an expression to a variable and is also called as Shorthand operators.

Syntax
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.

CODE/PROGRAM/EXAMPLE
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

Conditional Operators :

The character pair ?: is a ternary operator of Java, which is used to construct conditional expressions of the following form:

Syntax
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.

CODE/PROGRAM/EXAMPLE
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.

CODE/PROGRAM/EXAMPLE
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

Increment and Decrement Operators :

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.

CODE/PROGRAM/EXAMPLE
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 Operators :

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

CODE/PROGRAM/EXAMPLE
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

CODE/PROGRAM/EXAMPLE
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

OPERATOR PRECEDENCE IN JAVA :

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:

  • High priority (* / %)
  • Low priority (+ -)

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 :

CODE/PROGRAM/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
#operator_in_java #java_operator #Arithmetic_operators_in_java #Logical_operators_in_java #Relational_Operators_in_java #Assignment_Operators_in_java #Conditional_Operators_in_java #Increment_operator_in_java #Decrement_Operators_in_java #Bit_Wise_Operators_in_java #OPERATOR_PRECEDENCE_IN_JAVA

(New page will open, for Comment)

Not yet commented...