[go: up one dir, main page]

0% found this document useful (0 votes)
17 views26 pages

Java Operators

Uploaded by

riaz ahamed
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)
17 views26 pages

Java Operators

Uploaded by

riaz ahamed
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/ 26

JAVA

OPERATORS
Operator - Definition
Operator a symbol that is used to perform mathematical
or logical operations.
Classification of operators
 Binary Operators
 Unary Operators
 Ternary Operators
Classification of operators
An operator that performs an action on one operand is called
a unary operator (+, –, ++, – –).
An operator that performs an action on two operands is
called a binary operator (+, –, / , * , etc).
An operator that performs an action on three operands is
called a ternary operator (? :).
Binary Operators
Arithmetic Operators
Assignment Operators
Relational Operators
Logical Operators
Bitwise Operators
Shift Operators
Arithmetic Operators
Arithmetic operators are used for adding (+), subtracting (–), multiplying (*), dividing
(/), and finding the remainder (%).

Operator Meaning Example Result


+ Addition 3+4 7
- Subtraction 5-7 -2
* Multiplication 5*5 25
/ Division (gives quotient) 14 / 7 2
% Modulus (gives remainder) 20 % 7 6
Assignment Operators
The equal to operator (=) is said to be an assignment
operator.
It is used to assign a value to any variable.

Syntax: variable = value;


Example: int a=5;
Assignment Operator
The equal to operator (=) is said to be an assignment operator.
It is used to assign a value to any variable.

Syntax: variable = value;


Example: int a=5;
Shorthand Assignment Operators
An assignment operator combined with operators to build a
shorter version of statement is called a Shorthand Assignment
Operator.

Example:
a=a+5 can be written as a+=5
a=a-5 can be written as a-=5
a=a*5 can be written as a*=5
a=a/5 can be written as a/=5
a=a%5 can be written as a%=5
Relational Operators
Relational operators are used to compare two operands or
two expressions and the result is a Boolean.

Example: If x=10, y=20


Operator Meaning Example Result
== Equal to x==y false
!= Not equal to x!=y true
> Greater than x>y false
>= Greater than or equal to x >=y false
< Less than x<y true
<= Less than or equal to x<= y true
Logical Operators

These operators are used to perform logical AND and


logical OR operations on two Boolean expressions and
the result is a Boolean.

Example: If x=10, y=20


Operator Meaning Example Result
&& logical AND x>8 && y>8 true
|| logical OR x>10 || y>10 true
Bitwise Operators
Bitwise operators are used to perform manipulation of
individual bits of a number. They can be used with any of the
integers
Table for Bitwise Operators
A B A&B A|B A^B ~A
0 0 0 0 0 1
0 1 0 1 1 1
1 0 0 1 1 0
1 1 1 1 0 0
Bitwise Operators Example

Example: If a=5, b=7

Operator Meaning Example Result


& Bitwise AND a&b 7
| Bitwise OR a|b 5
Bitwise Operators

Bitwise AND
int x=5, y=6,z;
z= x & y;
variable 128 64 32 8 4 2 1
x 0 0 0 0 0 1 0 1
y 0 0 0 0 0 1 1 0
x&y 0 0 0 0 0 1 0 0

Result: z=4
Bitwise OR Operator

Bitwise OR
int x=5, y=6,z;
z= x & y;
variable 128 64 32 8 4 2 1
x 0 0 0 0 0 1 0 1
y 0 0 0 0 0 1 1 0
x|y 0 0 0 0 0 1 1 1

Result: z=7
Bitwise XOR Operator

Bitwise OR
int x=5, y=6,z;
z= x ^ y;
variable 128 64 32 16 8 4 2 1
x 0 0 0 0 0 1 0 1
y 0 0 0 0 0 1 1 0
x^y 0 0 0 0 0 0 1 1

Result: z=3
Bitwise NOT Operator
Bitwise NOT operator ~, also called as bitwise complement
inverts all the bits of the operand.

For example, the number 42, which has the following bit
pattern:
var 128 64 32 16 8 4 2 1

42
128
0
640 32 116 80 4 21 1 0 1 0

213 1 1 0 1 0 1 0 1

Result: z=3
Shift Operators
These Operators are used to shift the bits of a
number left or right .
Operator Description

<< Left Shift Operator

>> Signed right shift Operator

>>> Unsigned right shift Operator


Shift Operators - Left Shift
The left operand value is moved left by the no. of
bits mentioned by the right operand.
It has this general form: value << num
Example: If x=10, then calculate x<<2 value.
Shifting the value of x towards the left two positions
will make the leftmost 2 bits to be lost. The value of
x is 10. The binary representation of 10 is 00001010.
After shifting the bits to the left the binary number
00001010 (in decimal 10) becomes 00101000 (in decimal 40).
If x=63, then X << 2 will give 240 which is 1111 0000
Shift Operators - Right Shift

The right shift operator, >> shifts all of the bits in


a value to the right by a specified number of
times. It has this general form:
value >> num
Example: If x=10, then calculate x>>2 value.
Shifting the value of x towards the right two positions
will make the rightmost 2 bits to be lost.
The binary representation of 10 is 00001010.
After shifting the bits to the right the binary number
00001010 (in decimal 10) becomes
00000010 (in decimal 2).
Shift Operators –
Unsigned Right Shift
The left operand value is moved right by the no. of the bits defined
by a right operand and shifted values are filled up with zeroes.

Example: If x=10, then calculate x>>>2 value.


Shifting the value of x towards the right two positions
will make the rightmost 2 bits to be lost.
The binary representation of 10 is 00001010. After shifting
the bits to the right the binary number
00001010 (in decimal 10) becomes
00000010 (in decimal 2).
Unary Operators
Unary operators are operators that operate on a single
operand. These operators are used to perform various
operations in Java, such as negating a value,
incrementing a variable, or inverting a boolean value
Here are some common unary operators in Java:
1. Plus (+): Unary plus, indicates the positive value of a number.
Example: +51.
2. Minus (-): Unary minus, negates the value of a number.
Example: -51.
3. Not (!): Logical complement,
inverts the value of a boolean
Unary Operators
4.Complement (~): Bitwise complement, inverts all bits of a
number.
Example: ~5 == -6
5.Increment (++): Increments the value of a variable by
Example: x++
6.Decrement (--): Decrements the value of a variable by
Example: x—
the increment and decrement operators can be
used both before (prefix) and after (postfix) the variable.
The behavior differs slightly depending on the
position.
Example: ++x (prefix) vs x++.
Increment and Decrement Operators

Increment and decrement operators can be applied


to all integers and floating-point types.
They can be used either in prefix (– –x, ++x) or postfix (x– –,
x++) mode.
Prefix Increment/Decrement Operation
int x = 2;
int y = ++x; // x = 3, y = 3
int z = --x; // x = 1, z = 1
Postfix Increment/Decrement Operation
int x = 2;
int y = x++; // x == 3, y == 2
Ternary operators
Ternary operators are applied to three operands. This
conditional operator (? :) decides, on the basis of the first
expression, which of the two expressions to be evaluated.
Syntax: operand1? operand2: operand3
operand1 must be of boolean type or an expression producing
a boolean result. If operand1 is true, then operand2 is returned.
If operand1 is false, then operand3 is returned. This operator is
similar to an if conditional statement.
Ternary operators
Example:
String greater = x < y? "Y is greater”: “X is greater";

If the value of x is less than y, “Y is greater” string is retuned


and stored in the variable: greater,

else “X is greater” is retuned and stored in the variable:


greater.

You might also like