Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

UNIT-I

OPERATORS IN JAVA
 Operatorin JAVA is a Symbol that is used to
perform Operations.
 There are many types of Operators in JAVA.
Those are :
 Arithmetic Operators
1.
 2. Relational Operators
 3. Logical Operators
 4. Terinary Operators (or) Conditional
 5. Assignment Operators
 6. Increment / Decrement Operators
 7. Bitwise Operators
1. ARITHMETIC OPERATORS :
 ArithmeticOperators are used to perform
Mathematical Operations like Addition,
Subtraction, Multiplication, Division etc.
Operator Usage Example
+ a+b 5+3 = 8

- a-b 5-3 = 2

* a*b 5*3=
15
/ a/b 5 / 3 =1
Eg : /* Arithmetic Operators */
class arithmetic{  
public static void main(String args[]){  
int a=10;  
int b=5;  
System.out.println(a+b);//15  
System.out.println(a-b);//5  
System.out.println(a*b);//50  
System.out.println(a/b);//2  
System.out.println(a%b);//0  
}}  
2. RELATIONAL OPERATORS
 Relational
operators are used to compare
the two operands. Relational operators
always return Boolean value (true or false).
Operator Usage Example
> a>b 10>20= false
< a<b 10<20= true
>= a>=b 10>=20= false
<= a<=b 10<=20 = false
== a==b 10==20 =false
!= a!=b 10!=20 =True
Eg : /* Relational Operators */
class relational
{
public static void main(String args[])
{
int a = 10; int b = 20;
System.out.println("a == b = " + (a == b) );
System.out.println("a != b = " + (a != b) ); System.out.
println("a > b = " + (a > b) ); System.out.println("a < b
= " + (a < b) ); System.out.println("b >= a = " + (b >=
a) ); System.out.println("b <= a = " + (b <= a) );
}
}
3. LOGICAL OPERATORS
 Logicaloperators are used when we want
to check multiple conditions together.
 There are 3 types of Logical Operators .
Those are Logical AND, Logical OR, Logical
NOT. 
Operator Usage Example
&& a>b && a>c 2>3 && 2>4 = false
|| a>b || c>a 2>3 || 4>2=True
! !(a>b) !(2>3)= True
Eg : /* Logical Operators */
public class Logical
{
    public static void main (String [] args)
    {
       int x=6 ,y=4,z=5;
       System.out.println(" x>y && y>z=" +(x>y&& y>z));
       System.out.println(" x>y || y>z="  +  (x>y||y>z));
       System.out.println(" !(x>y)= " + (!(x>y)));  
    }
}
4. TERINARY OPERATORS (? :)
Conditional operator is also known as
the ternary operator. This operator
consists of three operands and is used
to evaluate Boolean expressions. 
Syntax :
Expression1 ? expression 2 : expression 3;
Ex:
a>b?a:b;
 first expression 1 is evaluated.
 If it is true then the expression2 is evaluated
 If it is false then the expression3 is evaluated
Eg : /* Terinary Operators */
class Test
{
public static void main(String args[])
{
int a, b; a = 10;
b = (a == 1) ? 20: 30;
System.out.println( "Value of b is : " + b );/
/30
b = (a == 10) ? 20: 30;
System.out.println( "Value of b is : " + b );/
/20
5. ASSIGNMENT OPERATORS (=)
 Assignment operator are used to assign the value
of an expression to a variable
 Combination of Assignment and Arithmetic
Operators is CalledShort Hand Operators

Operator Usage Example


+= 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
Eg : /* Assignment Operators */
class Assignment
{  
public static void main(String[] args)
{  
int a=10;  
a+=3;//10+3  
System.out.println(a);  
a-=4;//13-4  
System.out.println(a);  
a*=2;//9*2  
System.out.println(a);  
a/=2;//18/2  
System.out.println(a);  
}}  
6. INCREMENT / DECREMENT OPERATORS
 The Increment operator(++) adds 1 to the
operand
 The Decrement operator (--) subtracts 1
from operand
 Both areunary operators
 ++a and a++ mean the same thing when
they form statement independently, their
behave differently when they are used in
expressions on the right-hand side of an
assignment statement
a=5 a=5
b=++a b=a++
a=6 a=6
b=6 b=5
Eg 1: /*Increment / Decrement Operators
*/
class increment
{  
public static void main(String args[])
{  
int x=10;  
System.out.println(x++);//10 (11)  
System.out.println(++x);//12  
System.out.println(x--);//12 (11)  
System.out.println(--x);//10  
}
}  
Eg 2: /*Increment / Decrement Operators */

class operator
{  
public static void main(String args[])
{  
int a=10;  
int b=10;  
System.out.println(a++ + ++a);//10+12=22  
System.out.println(b++ + b++);//10+11=21   }
}  
7. BIT WISE OPERATORS
 Java defines several bitwise operators, which can
be applied to the integer types,long, int, short, char,
and byte.
 Bitwise operator works on bits and performs bit-by-
bit operation.
 Bitwise operators may not be applied to float or
double
 Assume if a = 60 and b = 13; now in binary format
they will be as follows :
Operator Description Example
& (bitwise Binary AND (A & B) will give 12 which is
and) Operator copies a 0000 1100
bit to the result if it
exists in both
operands.

| (bitwise or)Binary OR Operator (A | B) will give 61 which is


copies a bit if it 0011 1101
exists in either
operand.

^ (bitwise Binary XOR Operator (A ^ B) will give 49 which is


XOR) copies the bit if it is 0011 0001
set in one operand
but not both.
~ (bitwise Binary Ones (~A ) will give -61
compliment) Complement which is 1100 0011
Operator is unary and in 2's complement
has the effect of form due to a
'flipping' bits. signed binary
number.

<< (left shift) Binary Left Shift A << 2 will give 240
Operator. The left which is 1111 0000
operands value is
moved left by the
number of bits
specified by the right
operand.
>> (right shift) Binary Right Shift A >> 2 will give
Operator. The left 15 which is 1111
operands value is moved
right by the number of
bits specified by the right
operand.

>>> (zero fill Shift right zero fill A >>>2 will give
right shift) operator. The left 15 which is 0000
operands value is moved 1111
right by the number of
bits specified by the right
operand and shifted
values are filled up with
zeros.
Eg : /*Bit Wise Operators */
class bitwiseop {
public static void main(String[] args) {
//Variables Definition and Initialization
int num1 = 30, num2 = 6, num3 =0;
//Bitwise AND
System.out.println("num1 & num2 = " + (num1 &
num2));
//Bitwise OR
System.out.println("num1 | num2 = " + (num1 | num2) )
;
//Bitwise XOR
System.out.println("num1 ^ num2 = " + (num1 ^ num2)
);
Cont…
//Binary Complement Operator
System.out.println("~num1 = " + ~num1 );
//Binary Left Shift Operator
num3 = num1 << 2;
System.out.println("num1 << 1 = " + num3 );
//Binary Right Shift Operator
num3 = num1 >> 2;
System.out.println("num1 >> 1 = " + num3 );
//Shift right zero fill operator
num3 = num1 >>> 2;
System.out.println("num1 >>> 1 = " + num3 );
}
}

You might also like