Operators in java explaining different types .pptx

rekhakeerti19 8 views 36 slides Sep 09, 2024
Slide 1
Slide 1 of 36
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36

About This Presentation

Operators: Arithmetic Operators, Relational Operators, Boolean Logical Operators, The Assignment Operator, The ? Operator, Operator Precedence, Using Parentheses.


Slide Content

O OPERATORS IN JAVA
(2

OPERATOR

o An operator is a symbol that operates on one or more

arguments to produce a result.

o Java provides a rich set of operators to manipulate variables.

OPERANDS
o An operands are the values on which the operators act upon.
o An operand can be:

A numeric variable - integer, floating point or

character

Any primitive type variable - numeric and boolean
Reference variable to an object

A literal - numeric value, boolean value, or string.
An array element, "a[2]"

char primitive, which in numeric operations is ®

treated as an unsigned two byte integer

TYPES OF OPERATORS

1. Assignment Operators

2. Increment Decrement Operators
3. Arithmetic Operators

4, Bitwise Operators

5. Relational Operators

6. Logical Operators

7. Ternary Operators

8. Comma Operators

9. Instanceof Operators

ARITHMETIC OPERATORS

o The arithmetic operators are used to construct mathematical

expressions as in algebra.

o Their operands are of numeric type.

ARITHMETIC OPERATORS

Operator Result

+ Addition

- Subtraction (also unary minus)
= Multiplication

1 Division

% Modulus

++ Increment

+= Addition assignment

Subtraction assignment

Multiplication assignment

Division assignment

Modulus assignment

Decrement

The Modulus Operator

The modulus operator, %, returns the remainder of a division operation. It can be

applied to floating-point types as well as integer types. The following example program
demonstrates the %:

// Demonstrate the % operator.
class Modulus {
public static void main(String[] args) {
int x = 42;
double y = 42.25;

System.out .println("x mod 10 =" + x % 10);
System.out .println("y mod 10 Ma y *10)5

)

When you run this program, you will get the following output:

x mod 10 = 2
y mod 10 = 2.25

SIMPLE ARITHMETIC

public class Example {
public static void main(String[] args) {
int j, k, pP, a, x, 8, t;
J = 5),
2,

una x

AAA
SS


k;
System.out.printin("p = "
System.out.printin("q
System.out.printin("r
System.out.printin("s
System.out.printin("t =

+++++
we

java Example
=
=3
10
2
1

>

veanamv

ASSIGNMENT OPERATORS

o The assignment statements has the following syntax:

<variable> = <expression>

var = var op expression;

var op= expression;

ASSIGNING VALUES EXAMPLE

Assigning primitive value

// 2 is assigned to variable a
// 5 is assigned to variable b

Home homel = new Home(); // new object created
Home home2 = homel; // assigning the reference of homel in home2

// Demonstrate several assignment operators.
class OpEquals {
public static void main(String[] args) {

int a = 1;
int b = 2;
int e = 3;
a += 5;
b *= 4;
¢ += a * b:
a %= 6;

System.out.printin("a " + a);
System.out.printin("b = " + b);
System.out.printin("c = " + c);

INCREMENT AND DECREMENT OPERATORS
++ AND --

o The increment and decrement operators add an integer
variable by one.

o increment operator:

o two successive plus signs, ++

o decrement operator: --

INCREMENT AND DECREMENT OPERATORS
++ AND --

Common Shorthand
=a+l; at+; or ++a;
a=a-l; a--; or --a;

EXAMPLE OF ++ AND -- OPERATORS

public class Example
{

public static void main(String[] args)
{

BITWISE OPERATORS

o Java's bitwise operators operate on individual bits of integer

(int and long) values.

o If an operand is shorter than an int, it is promoted to int before

doing the operations.

FF

Bitwise unary NOT
& Bitwise AND
| Bitwise OR
4 Bitwise exclusive OR
>> Shift right
>>> Shift right zero fill
<< Shift left
&= Bitwise AND assignment
|= Bitwise OR assignment
As Bitwise exclusive OR assignment
S35 Shift right assignment
>>> Shift right zero fill assignment
<= Shift left assignment

The Bitwise Logical Operators

The Bitwise NOT
Also called the bitwise complement, the unary NOT operator, ~, inverts all of the bits of its
operand. For example, the number 42, which has the following bit pattern:

00101010
becomes
11010101
after the NOT operator is applied.

The Bitwise AND
The AND operator, &, produces a 1 bit if both operands are also 1. A zero is produced in all
other cases. Here is an example:

00101010 42
&00001111 15

00001010 10

The Bitwise OR
The OR operator, |, combines bits such that if either of the bits in the operands is a 1, then

the resultant bit is a 1, as shown here:

00101010 42
}00001111 15

00101111 47

BITWISE OPERATORS

Operator Name Description
men = 1 if both bits are 1.
alb = 1 if either bit is 1.
ES er 1 if both bits are different.
= a Inverts the bits.
Shifts the bits of n left p positions. Zero bits
n<<p left shitt are shifted into the low-order positions.
Shifts the bits of n right p positions. Ifn is a 2's
vo we mer
Shifts the bits of n right p positions. Zeros ai
n>>>p right shift shifted into the high-order positions. Li

EXAMPLE OF BITWISE OPERATORS

class Test {

public static void main(String argsl]) {
int a = 60; /* 60 = 0011 1100 */
int b = 13; /* 13 = 0000 1101 */
int c = 0;

c=a & b; /* 12 = 0000 1100 */
System.out.printin("a & b="+c );

c=a|b;/* 61 = 0011 1101 *
System.out.printin("a | b="+c );

EXAMPLE CONT.,

»

c=a*b;/* 49 = 0011 0001 */
System.out.printin("a * b ="+c );

= ~a; /*-61 = 1100 0011 */
System.out.printin("~a =" + c );

c=a<< 2; /* 240 = 1111 0000 */
System.out.printin("a << 2="+c);_

Output:
c=a >> 2," 215 = 1111 */ er
System.out.printin("a>> 2 =" +0); 21b=
ystem.out.printin("a Ch] dabeso
~a=-61

c=a >>> 2; /* 215 = 0000 1111 */ a<<2=240
System.out.printin("a>>>2="+cC)} a>>15a>>>15 O

RELATIONAL OPERATORS

o Arelational operator compares two values and determines the
relationship between them.

o For example, != returns true if its two operands are unequal.

o Relational operators are used to test whether two values are

equal, whether one value is greater than another, and so

forth.

RELATIONAL OPERATORS

Operator

Description

Checks if the value of two operands are equal or
not, if yes then condition becomes true.

Checks if the value of two operands are equal or

not, if values are not equal then condition becomes
true.

Checks if the value of left operand is greater than
the value of right operand, if yes then condition
becomes true.

RELATIONAL OPERATORS

Operator

Description

<

Checks if the value of left operand is less than
the value of right operand, if yes then condition
becomes true.

Checks if the value of left operand is greater than

or equal to the value of right operand, if yes then
condition becomes true.

Checks if the value of left operand is less than or

equal to the value of right operand, if yes then
condition becomes true.

EXAMPLE OF RELATIONAL OPERATORS

public LessThanExample

l public static void main(String args[])
E int a = 5; int b = 10;
if(a < b)
{
System.out.printin("a is less than b");
}
}

LOGICAL OPERATORS

o These logical operators work only on boolean operands. Their

return values are always boolean.

LOGICAL OPERATORS

| Operator Description

88 Called Logical AND operator. If both the
operands are non zero then then condition
becomes true.

il Called Logical OR Operator. If any of the two
operands are non zero then then condition
becomes true.

Ir Called Logical NOT Operator. Use to reverses
the logical state of its operand. If a condition is
true then Logical NOT operator will make false.

EXAMPLE OF LOGICAL OPERATORS
public class ANDOperatorExample(
public static void main(String[] args){
char ans = 'y';
int count = 1;

System.out.printin("Count is Zero.");}

'y' & count == 1) {
System.out.printin("Count is One."); }

if(ans == 'y' & count == 2) {
System.out.printin("Count is Two.");

}}}

TERNARY OPERATORS

o Java has a short hand way by using ?: the ternary aka

conditional operator for doing ifs that compute a value.

o Unlike the if statement, the conditional operator is an

expression which can be used for

EXAMPLE OF TERNARY OPERATOR

// longhand with if:
int answer;
if(a>b)
{
answer = 1;
}
else
{
answer = -1;

// can be written more tersely with the ternary operator as: int answer

sa>b?1:-

COMMA OPERATORS

o Java has an often look past feature within it’s for loop and this
is the comma operator.

o Usually when people think about commas in the java language
they think of a way to split up arguments within a functions

parameters

EXAMPLE OF COMMA OPERATOR

11: c03:CommaOperator.java

11 From ‘Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002

I www.BruceEckel.com. See copyright notice in CopyRight.txt.
public class CommaOperator {

public static void main(String[] args) {

for(int i= 1,j=i+10;i<5;
i++, j=i* 2){
‘System.out.printin("i= * + i +" j= "+ j);

}

}
ll

INSTANCEOF OPERATORS

o This operator is used only for object reference variables. The
operator checks whether the object is of a particular type(class

type or interface type).

o InstanceOf operator is wriiten as:

( Object reference variable ) instanceOf (class/interface type)

EXAMPLE OF INSTANCEOF OPERATOR

class Vehicle {}
public class Car extends Vehicle

{
public static void main(String args[])
{
Vehicle a = new Car();
boolean result = a instanceof Car;
System.out.printin( result);
}
}

THE END