You are on page 1of 10

CHAPTER 20

OPERATORS in JAVA

An operator is a symbol that performs an operation. An operator acts on


some variables called operands to get the desired result.

Operand: It is the value on which operators are worked. We can supply


the required value to the operator either by using literal, or variable or
expression or non-void method. That is, an operator performs an
operations on operands.

Operator

a + b

Operand operand

Java operators can be classified into three types based on operands

1. Unary(perform operation on one operand)


2. Binary(two operands)
3. Ternary(three operands)
If an operator acts on a single variable, it is called unary operator, if it
acts on two variables, it is called binary operator. Now let us examine
various types of operators in detail.
Types of operators:
1. Assignment Operator:
 An assignment operation assigns the value of the right-hand operand to
the storage location named by the left-hand operand.
 The left-hand operand of an assignment operation must be a modifiable
l-value.
 The right side data to the assignment operator may be a value, a
constant expression or a method call.
 Ex:- a=a-2; n=n*m; t=t+m2(78);

Arithmetic Operators:-

      Arithmetic Operators are used to perform some mathematical


operations like addition, subtraction, multiplication, division, and modulo (or
remainder). These are generally performed on an expression or operands.
The symbols of arithmetic operators are given in a table:

Symbol Name of the Operator Example


 + Additive Operator   n =  n + 1;
 - Subtraction Operator  n =  n - 1;

 * Multiplication Operator   n =  n * 1;

 / Division Operator  n = n / 1;
 % Remainder Operator  n = n % 1;

The "+" operator can also be used to concatenate (to join) the two
strings together. For example:

String str1 = "Sravan";

String str2 = "Kumar";

String result = str1 + str2;

 The variable "result" now contains a string "SravanKumar".

Let’s have one more example implementing all arithmetic operators:


Example:
class ArithmeticDemo
{
public static void main(String[] args)
{
int x = 4;
int y = 6;
int z = 10;
int rs = 0;
rs = x + y;
System.out.println("The addition of (x+y):"+ rs);
rs = y - x;
System.out.println("The subtraction of (y-x):"+ rs);
rs = x * y;
System.out.println("The multiplication of (x*y):"+ rs);
rs = y / x;
System.out.println("The division of (y/x):"+ rs);
rs = z % y;
System.out.println("The remainder of (z%x):"+ rs);
rs = x + (y * (z/x));
System.out.println("The result is now :"+ rs);
}
}
Example:

class AddingTwoInts
{
public static void main(String[] args)
{
int a = 60;
int b = 70;
int c = a + b;
System.out.println("The addition of " + a + " and " + b + " is " + c);
}
}
Example:

class Ex
{
public static void main(String[] args)
{
int a = 10; int b = 20;
System.out.println(a + b);
System.out.println("a" + "b");
System.out.println("a + b" + a + b);
System.out.println("a + b" + 10 + b);
System.out.println("a + b" + 10 + 20);
System.out.println("a + b" + "10" + 20);
System.out.println("a + b10" + 20);
System.out.println("a + b10" + "20");
System.out.println("a + b1020");
System.out.println("a + b" + (a + b));
System.out.println("a + b" + (10 + 20));
System.out.println("a + b" + 30);
System.out.println("a + b" + "30");
System.out.println("a + b30");

//System.out.println("a - b" + a - b);


System.out.println("a - b" + (a - b));//a-b-10
System.out.println("a * b" + a * b);

//more testing bits


System.out.println(10 + 20);
System.out.println(10 + 20 + "");
System.out.println(10 + "" + 20 );
System.out.println("" +10 + 20 );
System.out.println("" +(10 + 20) );
}
}
3. Conditional Operator:
 Conditional operator is also called as 'ternary operator'.
 It is widely used to execute condition in true part or in false part.
 It operates on three operands called as ternary operator.
 The logical or relational operator can be used to check conditions.

Example:
class LO
{
public static void main(String[] args)
{
int a=5, b=3,c;

c=(a>b)?a:b;
System.out.println(c);
};
}

4. Unary Operators :( modifying operators):

 It operates on a single operand. Therefore, this operator is called as


'unary operator.'
 These are also called increment and decrement operators.
 Increment operators are again divided into pre-increment operators and
post-increment operators. Decrement operators are also categorized into
pre-decrement and post-decrement operators.
 Increment operators (either pre-incre or post-incre) increase the value of
a variable by 1only but when pre-increment operators increase the
value and post-increment increases the value matters.
 Decrement operators (either pre-decre or post-decre) decrease the value
of a variable by 1.
Difference between pre-increment and post-increment variable:

They both increment the variable. But the value returned by the pre-
increment operator is the value of the variable after it has been
incremented, while the value returned by the post-increment operator is
the value before it has been incremented.

ForExample:
class LO
{
public static void main(String[] args)
{
int a = 1;
int b = ++a;
// Now a is 2 and b is also 2.
System.out.println(a);System.out.println(b);
int c = 1;
int d = c++;
// Now c is 2 but d is 1.
System.out.println(c);System.out.println(d);

}
}
5. Relational Operators
 The relational operators compare two operands and determine the
validity of a relationship.
 The relational operator returns 1 if condition is true else return 0.

Operator Symb Form Operation


ol
Greater than > x > true if x is greater than y, false
y otherwise
Less than < x < true if x is less than y, false
y otherwise
Greater than or >= x>=y true if x is greater than or equal to
equals y, false otherwise
Less than or <= x <= true if x is less than or equal to y,
equals y false otherwise
Equality == x == true if x equals y, false otherwise
y
Inequality != x != true if x does not equal y, false
y otherwise

Example:
class X{
public static void main(String args[])
{
int a=5, b=3,c=7,d=5;
boolean e,f,g,h;
e = a++ != d--;
f = ++b == d++;
g = c-- >= ++d;
h = ++d >= ++a;
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
System.out.println(f);
System.out.println(g);
System.out.println(h);
}}

6. Logical Operators:
 It is the operator which is primarily used to check more than one
condition.
 These operator returns 1 if condition is true else return 0.
 The operators are &&,||,!

Operator Description
&& Called Logical AND operator. If both the operands are
non zero then condition becomes true.
|| Called Logical OR Operator. If any of the two operands
is non zero then condition becomes true.
! Called Logical NOT Operator. Use to reverses the logical
state of its operand. If a condition is true then Logical
NOT operator will make false.
The below table represents the truth table of logical operators:
| T-true F-false

When ‘A’ is When ‘B’ is A&&B is A||B is !A is !B is


T T T T F F

F T F T T F
T F F T F T
F F F F T T

Class Y
{
public static void main(String args[]))
{
int a=5,b=3,c=2,d=3;
boolean e,f,g,h;
e = (b++ == ++c) || (--b != d--);
f = (b-- != d++) && (--c <= --a);
g = (c++ >= --d) || (++b != --c);
h = (d++ <= --c) && (++b!= --c);
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
System.out.println(f);
System.out.println(g);
System.out.println(h);
}}
7. Compound assignment Operators:
 Compound assignment operator can reduce the size of an expression
in the application.
 It is used to perform mathematical operations at which the result or
output can affect on operands.

For example b=b+a will be written as b += a ;

8. Bitwise Operators:
 Bitwise operators are used for manipulation of data at a bit level.
 They can be directly applied to char, short int and long.
 The operators are &, |, ^.
 These operators first convert the data into corresponding binary form
and then perform the operations according to truth table.

Truth table:

a b a&b a|b a^ b ~a ~b
0 0 0 0 0 1 1
0 1 0 1 1 1 0
1 0 0 1 1 0 1
1 1 1 1 0 0 0

Program:
Class Z
{
public static void main(String args[]))
{
int a=15,b=8,c,d,e;
c = a & b ; d = a | b ; e = a ^ b ;
System.out.println(c); System.out.println(d); System.out.println(e);
}
9. Shift Operators

 The bitwise shift operators move the bit values of a binary object.
 The left operand specifies the value to be shifted.
 The right operand specifies the number of positions that the bits in
the value are to be shifted.
 The bit shift operators take two arguments, and looks like:
x << n x >> n
Where x can be any kind of int variable or char variable and n can be
any kind of int variable.

class W
{
public static void main(String args[]))
{
int a=8,b,c;
b = a << 2 ;
c = a >> 2 ;
System.out.println(b);
System.out.println(c);
}
}

Theoretical formulas:
There are some theoretical formulas to shift the bits either to right side
or to left side.
For right shifting : n/2s
For left shifting : n*2s
Where ‘n’ is the data that you are going to shift
‘s’ is the number of shifting bits

You might also like