Professional Documents
Culture Documents
20-CHAPTER20 Operators
20-CHAPTER20 Operators
OPERATORS in JAVA
Operator
a + b
Operand operand
Arithmetic Operators:-
* 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:
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");
Example:
class LO
{
public static void main(String[] args)
{
int a=5, b=3,c;
c=(a>b)?a:b;
System.out.println(c);
};
}
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.
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
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.
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