0% found this document useful (0 votes)
5 views23 pages

Operators Oriented Java

ppt lecture slides

Uploaded by

Yash Bindra
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)
5 views23 pages

Operators Oriented Java

ppt lecture slides

Uploaded by

Yash Bindra
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

OBJECT ORIENTED

PROGRAMMING
USING JAVA
◾ Operators
◾ Unary operators
◾ Arithmetic operators
OUTLINE ◾ Bitwise operators
◾ Ternary operators
◾ Assignment Operators
O PERATO RS
❑ Operator in Java is a symbol that is used to perform operations.
For example: +, -, *, / etc.
❑ There are many operators in Java:
❖Unary operators
❖Arithmetic operators
❖Bitwise operators
❖Ternary operators
❖Assignment operators
UNARY OPERATORS

The Java unary operators require only one operand. Unary


operators are used to perform various operations i.e.:

•incrementing/decrementing a value by one


•negating an expression
EXAMPLE:UNARY OPERATORS

public class OperatorExample{


public static void main(String args[]){ OUTPUT:
int x=10; 10
System.out.println(x++); 12
System.out.println(++x); 12
System.out.println(x--); 10
System.out.println(--x);
}}
PRACTICE QUESTION -1
public class OperatorExample{
public static void main(String args[])
{ OUTPUT:
int a=10; 22
int b=10; 21
boolean c=true; false
System.out.println(a++ + ++a);
System.out.println(b++ + b++);
System.out.println(!c);

}
}
ARITHMETIC OPERATORS

Java arithmetic operators are used to perform addition,


subtraction, multiplication, and division. They act as basic
mathematical operations.
EXAMPLE:ARITHMETIC OPERATORS
public class OperatorExample{
public static void main(String args[]){
int a=10; OUTPUT:
int b=5; 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
System.out.println(a%b);
}}
PRACTICE QUESTION -1
public class OperatorExample{
public static void main(String[] args){
int a=10; OUTPUT:
a+=3; 13
System.out.println(a);
a-=4;
9
System.out.println(a); 18
a*=2; 9
System.out.println(a);
a/=2;
System.out.println(a);
}}
PRACTICE QUESTION -2

public class OperatorExample{ OUTPUT:


public static void main(String args[]){ 21
System.out.println(10*10/5+3-1*4/2);
}}
BITWISE OPERATORS

Java bitwise operators are used to compare each bit of the first
operand to the corresponding bit of the second operand.
EXAMPLE:LOGICAL AND BITWISE &
AND | OPERATORS
public class OperatorExample{
public static void main(String args[]){ OUTPUT:
int a=10; false
int b=5; false
int c=20; true
System.out.println(a<b&&a++<c); true
System.out.println(a<b&a++<c);
System.out.println(a>b||a<c);
System.out.println(a>b|a<c);
}}
EXAMPLE:LEFT AND RIGHT SHIFT
BITWISE OPERATORS
public OperatorExample{ OUTPUT:
public static void main(String args[]){ 40
System.out.println(10<<2); 80
System.out.println(10<<3); 80
System.out.println(20<<2); 240
System.out.println(15<<4);
2
System.out.println(10>>2);
System.out.println(20>>2); 5
System.out.println(20>>3); 2
}}
PRACTICE QUESTION -1

public OperatorExample{
public static void main(String args[]) OUTPUT:
{ 2
System.out.println(10>>2);
System.out.println(20>>2);
5
System.out.println(20>>3); 2
}
}
PRACTICE QUESTION -2
public class OperatorExample{
public static void main(String args[]){ OUTPUT:
int a=10; false
int b=5; 10
int c=20;
false
System.out.println(a<b&&a++<c);
System.out.println(a); 11
System.out.println(a<b&a++<c);
System.out.println(a);
}}
TERNARY OPERATORS

Java ternary operator is used as one line replacement for if-


then-else statement and used a lot in Java programming. It is
the only conditional operator which takes three operands.
EXAMPLE:TERNARY OPERATORS

public class OperatorExample{


public static void main(String args[]){ OUTPUT:
int a=2; 2
int b=5;
int min=(a<b)?a:b;
System.out.println(min);
}}
ASSIGNMENT OPERATORS

Java assignment operator is one of the most common operators.


It is used to assign the value on its right to the operand on its
left.
EXAMPLE:ASSIGNMENT OPERATORS

public class OperatorExample{


public static void main(String args[]){ OUTPUT:
int a=10; 14
int b=20; 16
a+=4;
b-=4;
System.out.println(a);
System.out.println(b);
}}
PRACTICE QUESTION -1
public class OperatorExample{
public static void main(String[] args){
int a=10; OUTPUT:
a+=3; 13
System.out.println(a);
a-=4;
9
System.out.println(a); 18
a*=2; 9
System.out.println(a);
a/=2;
System.out.println(a);
}}
Operator Precedence
Operator Type Associativity
++ Unary post-increment Right to left
-- Unary post-decrement
++ Unary pre-increment Right to left
-- Unary pre-decrement
+ Unary plus
- Unary minus
! Unary logical negation
~ Unary bitwise complement
* Multiplication Left to right
/ Division
% Modulus
+ Addition Left to right
- Subtraction
<< Bitwise left shift Left to right
>> Bitwise right shift with sign extension
>>> Bitwise right shift with zero extension
Operator Precedence
Operator Type Associativity
< Relational less than Left to right
<= Relational less than or equal
> Relational greater than
>= Relational greater than or equal
== Relational is equal to Left to right
!= Relational is not equal to
& Bitwise AND Left to right
^ Bitwise exclusive OR Left to right
| Bitwise inclusive OR Left to right
&& Logical AND Left to right
|| Logical OR Left to right
?: Ternary conditional Right to left
= Assignment Right to left
+= Addition assignment
-= Subtraction assignment
*= Multiplication assignment
THANK YOU

You might also like