0% found this document useful (0 votes)
10 views

Operators

Uploaded by

Aditya Kaushal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Operators

Uploaded by

Aditya Kaushal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

OPERATORS

Operators
 Operators are particular symbols that are used
to perform operations on operands.
Example:
4+5=9
➢Here 4 and 5 are Operands and (+) , (=) signs
are the operators.
➢This expression produces the output 9.
Types of operators
 Arithmetic Operators
 Relational operators
 Logical operator
 Ternary Operator
 Assignment operators
 unary operators
 Bitwise operators
 Shift Operator
Arithmetic Operators
 Java arithmatic operators are used to perform addition,
subtraction, multiplication, and division. .
Operator
Addition(+):
Adds values on either side of the operator.
Subtraction(-):
Subtracts right-hand operand from left-hand operand.
Multiplication(*):
Multiplies values on either side of the operator.
Division(/):
Divides left-hand operand by right-hand operand.
Modulus(%):
Divides left-hand operand by right-hand operand and returns remainder.
Ex:
java.util.Scanner;
public class Arithmetic
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int a, b ,c, d ,e ,f, g;
System.out.println("Enter the value of a and b");
a=sc.nextInt();
b=sc.nextInt();
c=a+b;
System.out.println(c);
d=a-b;
System.out.println(d);
e=a*b;
System.out.println(e);
f=a/b;
System.out.println(f);
g=a%b;
System.out.println(g);
}
}
Relational operators
 The Java Relational operators compare between operands and
determine the relationship between them.
 The outcome of these operations is a boolean value.
Ex:
public class Relational
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a,b;
System.out.println("Enter the value of a and b");
a=sc.nextInt();
b=sc.nextInt();
System.out.println(a<b);
System.out.println(a>b);
System.out.println(a<=b);
System.out.println(a>=b);
System.out.println(a==b);
System.out.println(a!=b);
}
Logical operator
 These operators are used to perform logical operations on
the given expressions.
 They are, logical AND (&&), logical OR (||) and logical
NOT (!).
Operators Example/Description

&& (logical It returns true when both conditions


AND) are true
|| (logical It returns true when at-least one of
OR) the condition is true
! (logical It reverses the state of the operand
NOT)
class LogicalOperator
{
public static void main(String[] args)
{
int n1 = 1, n2 = 2, n3 = 9;
boolean result1,result2,result3;
result1 = (n1 > n2) || (n3 > n1);
System.out.println(result1);
result2 = (n1 > n2) && (n3 > n1);
System.out.println(result2);
result3=!(n2<n3)
System.out.println(result3);
}
Ternary Operator
 The ternary operator ?: is short hand for if-then-else
statement.
The syntax of conditional operator is:
 variable = Expression ? expression1 : expression

Here's how it works.


 If the Expression is true, expression1 is assigned to variable.

 If the Expression is false, expression2 is assigned to variable.


Example:
class OperatorExample
{
public static void main(String args[])
{
int a=2;
int b=5;
int min=(a<b)?a:b;
System.out.println(min);
}
}
Assignment operators
 Assignment operators are used to assigning values to the left
operand.
 The assignment operator denoted by the single equal sign =

Operator Example Equivalent to

+= X+= 5 X=X+5

-= X-=5 X=X-5

*= X*=5 X=X*5

/= X/=5 X=X/5

%= X%=5 X=X%5
public class Assign
{
public static void main(String[] args)
{
int a=12,b=10,c=4,d=12,e=15;
a+=15;
b-=2;
c*=3;
d/=4;
e%=2;
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
}
}
unary operators
 Java, unary arithmetic operators are used to increasing or
decreasing the value of an operand.

Operator Description

val = a++; Store the value of "a" in "val" then increments.

val = a--; Store the value of "a" in "val" then decrements.

val = ++a; Increments "a" then store the new value of "a" in
"val".

val = --a; Decrements "a" then store the new value of "a" in
"val".
public class Unaryop
{
public static void main(String[] args)
{
int r = 6;
System.out.println("r=: " + r++);
System.out.println("r=: " + r);
int x = 6;
System.out.println("x=: " + x--);
System.out.println("x=: " + x);
int y = 6;
System.out.println("y=: " + ++y);
int p = 6;
System.out.println("p=: " + --p);
}
}
Bitwise operator
 Bitwise java operators are used to perform operations on single
bitwise values.

Operator Description

& Bitwise AND

Bitwise inclusive OR
|

Bitwise exclusive OR
^
Bitwise AND(&):
 The & operator compares corresponding bits of two operands.
 If both bits are 1, it gives 1 else 0.
Syntax:
var1&var2
Ex:
public class Bitand {
public static void main(String[] args)
{
int x=9,y=8;
int z=x&y;
System.out.println(z);
}
}
Bitwise OR(|):
 The | operator compares corresponding bits of two operands.
 If either of the bits is 1, it gives 1 else 0.
Syntax:
var1|var2
Ex:
public class Bitor{
public static void main(String[] args)
{
int x=5,y=3;
int z=x|y;
System.out.println(z);
}
}
Bitwise EOR(^):
 The ^ operator compares corresponding bits of two operands.
 If corresponding bits are different, it gives 1 else 0.
Syntax:
Var1^var2
Ex:
public class Biteor
{
public static void main(String[] args)
{
int x=5,y=3;
int z=x^y;
System.out.println(z);
}
}
Shift Operator
 A shift operator performs bit manipulation on data by shifting
the bits of its first operand right or left.
Types:
 left shift operators
 right shift operators
left shift operators
 Shifts the value to the left which specified by the right
operand.
Syntax:
 bit pattern<< number of shifts
EX:
public class Leftshift
{
public static void main(String[] args)
{
int x=3,y=2;
int z=x<<y;
System.out.println(z);
}
}
Right shift operators
 Shifts the value to the right which specified by the left
operand.
Syntax:
 bit pattern >> number of shifts
Ex:
public class condition
{
public static void main(String[] args) {
int x=3,y=2;
int z=x>>y;
System.out.println(z);
}
}

You might also like