Operators
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
+= 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; 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 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);
}
}