0% found this document useful (0 votes)
19 views34 pages

3 Operators

Details about operators in java

Uploaded by

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

3 Operators

Details about operators in java

Uploaded by

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

OPERATORS IN JAVA

Operators in java

Operator in java is a symbol that is used to perform operations


Java Operator
Operator Type Category Precedence
postfix expr++ expr--
Unary
prefix ++expr --expr +expr -expr ~ !
multiplicative */%
Arithmetic
additive +-
Shift shift << >>
comparison < > <= >= instanceof
Relational
equality == !=
bitwise AND &
Bitwise bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
Logical
logical OR ||
Ternary ternary ?:

Assignment assignment = += -= *= /= %=
Java Unary Operator

class OperatorExample{
public static void main(String args[]){
int x=10;
System.out.println(x++); 10
System.out.println(++x); 12
12
System.out.println(x--);
System.out.println(--x); 10

}}
SHIFT OPERATOR
Shift Operator

• Left Shift (<<)


• Right Shift (>>)
Left Shift Operator

• Left shift operator shifts all bits towards the left by a certain number of

specified bits.

• It is denoted by <<.
Java Left Shift Operator Example

class OperatorExample{
public static void main(String args[]){
System.out.println(10<<2); //10*2^2=10*4=40
System.out.println(10<<3); //10*2^3=10*8=80
System.out.println(20<<2); //20*2^2=20*4=80
System.out.println(15<<4); //15*2^4=15*16=240
}}
Right Shift Operator

• Right shift operator shifts all bits towards the right by a certain number of
specified bits.
• It is denoted by >>.

• When we shift any number to the right, the least significant bits (rightmost) are
discarded and the most significant position (leftmost) is filled with the sign bit.
Java Right Shift Operator Example

class OperatorExample{
public static void main(String args[]){
System.out.println(10>>2); //10/2^2=10/4=2
System.out.println(20>>2); //20/2^2=20/4=5
System.out.println(20>>3); //20/2^3=20/8=2
}}
Relation operators
Relation operators

• Used to test comparison between operands or values.


• Can be use to test whether two values are equal or not equal or less than or
greater than etc.
• Returns the Boolean results, i.e. true or false after the comparison
Operator Description

== Check if two operand are equal

!= Check if two operand are not equal.

> Check if operand on the left is greater than operand on the right

< Check operand on the left is smaller than right operand

>= check left operand is greater than or equal to right operand

<= Check if operand on left is smaller than or equal to right operand


Example

int a, b;
a=40;
b=30;
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) );
Bitwise operators
Bitwise operators

• Bitwise operators are used to perform operations bit by bit.


• Java defines several bitwise operators that can be applied to the integer types
long, int, short, char and byte.
Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< left shift
>> right shift
truth table for bitwise &, | and ^

a b a&b a|b a^b


0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
Bitwise OR Operator

12 = 00001100 (In Binary)


25 = 00011001 (In Binary)

Bitwise OR Operation of 12 and 25


00001100
| 00011001
____________
00011101 = 29 (In Decimal)
Program 1

class Main {
public static void main(String[] args) {

int number1 = 12, number2 = 25, result;

result = number1 | number2;


System.out.println(result); // prints 29
}
}
Bitwise AND Operator

12 = 00001100 (In Binary)


25 = 00011001 (In Binary)

// Bitwise AND Operation of 12 and 25


00001100
& 00011001
____________
00001000 = 8 (In Decimal)
Program

class Main {
public static void main(String[] args) {

int number1 = 12, number2 = 25, result;

// bitwise AND between 12 and 25


result = number1 & number2;
System.out.println(result); // prints 8
}
}
Bitwise XOR Operator

12 = 00001100 (In Binary)


25 = 00011001 (In Binary)

// Bitwise XOR Operation of 12 and 25


00001100
^ 00011001
____________
00010101 = 21 (In Decimal)
Program

class Main {
public static void main(String[] args) {

int number1 = 12, number2 = 25, result;

// bitwise XOR between 12 and 25


result = number1 ^ number2;
System.out.println(result); // prints 21
}
}
Bitwise Complement Operator

• Bitwise complement operator is a unary operator (works with only one


operand).
• It is denoted by ~.
• It changes binary digits 1 to 0 and 0 to 1.
• The bitwise complement of any integer N is equal to - (N + 1).

For example,
• Consider an integer 35.
• As per the rule, the bitwise complement of 35 should be -(35 + 1) = -36.
Program

class Main {
public static void main(String[] args) {

int number = 35, result;

// bitwise complement of 35
result = ~number;
System.out.println(result); // prints -36
}
}
Logical Operators
Logical operators

• Logical operators are used to check whether an expression is true or false.


• They are used in decision making.
Operator Example Meaning

&& (Logical expression1 && true only if both expression1


AND) expression2 and expression2 are true

expression1 || true if either expression1 or


|| (Logical OR)
expression2 expression2 is true

true if expression is false and


! (Logical NOT) !expression
vice versa
class Main {
public static void main(String[] args) {

// && operator
System.out.println((5 > 3) && (8 > 5)); // true
System.out.println((5 > 3) && (8 < 5)); // false

// || operator
System.out.println((5 < 3) || (8 > 5)); // true
System.out.println((5 > 3) || (8 < 5)); // true
System.out.println((5 < 3) || (8 < 5)); // false

// ! operator
System.out.println(!(5 == 3)); // true
System.out.println(!(5 > 3)); // false
}
}
Java Ternary Operator

•The ternary operator (conditional operator) is shorthand for the if-then-


else statement.
variable = Expression ? expression1 : expression2

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


• If the Expression is false, expression2 is assigned to the variable.
Example

int time = 20;

String result = (time < 18) ? "Good day." : "Good evening.";

System.out.println(result);

You might also like