0% found this document useful (0 votes)
8 views

Java Unit 1.2 (Operators)

The document discusses Java operators including arithmetic, assignment, relational, logical, and bitwise operators. It provides examples of using different operators and explains the order of operations. Key operator types and their uses are defined.

Uploaded by

Saranya
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Java Unit 1.2 (Operators)

The document discusses Java operators including arithmetic, assignment, relational, logical, and bitwise operators. It provides examples of using different operators and explains the order of operations. Key operator types and their uses are defined.

Uploaded by

Saranya
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 42

Chapter 2: Java Fundamentals

Operators
Operators

Group of Operators
• Arithmetic Operators
 Assignment Operator
 Order of Precedence
 Increment/Decrement Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
Operators
• Operators are special symbols used for:
– mathematical functions
– assignment statements
– logical comparisons
• Examples of operators:
–3+5 // uses + operator
– 14 + 5 – 4 * (5 – 3) // uses +, -, * operators
• Expressions: can be combinations of variables
and operators that result in a value
1.Java Arithmetic Operators
 Arithmetical operator are used in mathematical
expressions
 Can not use Operands on Boolean type, use either
numeric or char type
Addition +
Subtraction –
Multiplication 
Division /
Remainder (modulus ) %
Arithmetic Operators
• The following table summarizes the
arithmetic operators available in Java.

This
Thisisisan
aninteger
integerdivision
division
where
where the fractionalpart
the fractional part
isistruncated.
truncated.
Example
Example of division issues:
10 / 3 gives 3
10.0 / 3 gives 3.33333

As we can see,
•if we divide two integers we get an integer
result.
•if one or both operands is a floating-point
value we get a floating-point result.
Modulus

Generates the remainder when you


divide two integer values.
5%3 gives 2 5%4 gives 1
5%5 gives 0 5%10 gives 5
Modulus operator is most commonly used
with integer operands. If we attempt to
use the modulus operator on floating-point
values we will garbage!
1.1Basic Assignment Operator
• We assign a value to a variable using the basic
assignment operator (=).
• Assignment operator stores a value in memory.
• The syntax is
leftSide = rightSide ;

Allways
Allwaysititisisaa eitheraaliteral
ItItisiseither literal| |aa
variable variable
variableidentifier
identifier| |
variableidentifier.
identifier. ananexpression.
expression.
Examples:
i = 1;
start = i;
sum = firstNumber + secondNumber;
avg = (one + two + three) / 3;
int x,y,z;
x=y=z=100;
The Right Side of the
Assignment Operator
• The Java assignment operator assigns
the value on the right side of the
operator to the variable appearing on
the left side of the operator.
• The right side may be either:
• Literal: ex. i = 1;
• Variable identifier: ex. start = i;
• Expression: ex. sum = first + second;
Assigning Literals
• In this case, the literal is stored in the
space memory allocated for the variable
at the left side. A.
A.Variables
Variablesare
allocated in
are
memory.
allocated in memory.

firstNumber 11
A
A
secondNumber ???
???
int firstNumber=1, secondNumber;
firstNumber = 234;
secondNumber = 87; B
B B.
B. Literals
Literalsare
are
assigned
assignedtotovariables.
variables.

firstNumber 234
Code secondNumber 87

State of Memory
Assigning Variables
• In this case, the value of the variable at
the right side is stored in the space
memory allocated for the variable at the
left side. A.
A.Variables
Variablesare
are
allocated in memory.
allocated in memory.

firstNumber 11
A
A
i ???
???
int firstNumber=1, i;
firstNumber = 234;
i = firstNumber; B
B B.
B. values
valuesare
areassigned
assigned
totovariables.
variables.

firstNumber 234
Code i 234

State of Memory
Assigning Expressions
• In this case, the result of the evaluation of
the expression is stored in the space
memory allocated for variable at the left
side. A.
A.Variables
Variablesare
are
allocated
allocatedininmemory.
memory.

first 11 second ???


???
A
A
int first, second, sum; sum ???
???
first = 234;
second = 87; B
B B.
B. Values
Valuesare
are
Sum = first + second assigned
assignedtotovariables.
variables.

first 234
234 second 87
87
Code
sum 321
321

State of Memory
Updating Data
A.
A.The
Thevariable
variable
isisallocated
allocatedinin
memory.
memory.
number ???
???

B.
B.The
Thevalue
value237
237
isisassigned
assignedtoto
number.
number
number.
number

number 237
int number; A
A
number = 237; B
B C.
C.The
Thevalue
value35
35
number = 35; C
C overwrites
overwritesthe
previous
the
previous value237.
value 237.

number 35

Code State of Memory


Example: Sum of two integer
public class Sum {

// main method
public static void main( String args[] ){
int a, b, sum;
a = 20;
b = 10;
sum = a + b;
System.out.println(a + ” + ” + b + “ = “ + sum);

} // end main

} // end class Sum


Arithmetic/Assignment Operators

Java allows combining arithmetic and


assignment operators into a single operator:

Addition/assignment +=
Subtraction/assignment =
Multiplication/assignment =
Division/assignment /=
Remainder/assignment %=
Arithmetic/Assignment Operators
• The syntax is
eitheraaliteral
ItItisiseither literal| |aa
leftSide Op= rightSide ; variable
variableidentifier
identifier| |
ananexpression.
expression.

Allways
Allwaysititisisaa
variable
variableidentifier.
identifier. anarithmetic
ItItisisan arithmetic
operator.
operator.

• This is equivalent to:


leftSide = leftSide Op rightSide ;

• x%=5;  x = x % 5;
• x*=y+w*z;  x = x*(y+w*z);
1.2 Order of Precedence

( ) evaluated first, inside-out

, /, or % evaluated second, left-to-right

+,  evaluated last, left-to-right


Operators Precedence

Parentheses (), inside-out


Increment/decrement ++, --, from left to right
Multiplicative *, /, %, from left to right
Additive +, -, from left to right
Relational <, >, <=, >=, from left to right
Equality ==, !=, from left to right
Logical AND &&
Logical OR ||
Assignment =, +=, -=, *=, /=, %=
1.3 Increment/Decrement Operators

Only use ++ or   when a variable


is being incremented/decremented
as a statement by itself.
x++; is equivalent to x = x+1;

x--; is equivalent to x = x-1;


Example:

class IncrementDemo{
public static void main(String [] args){
int a=1;
System.out.println(a++);
System.out.println(a++);
System.out.println(++a);

System.out.println(a++);
System.out.println(a++);

System.out.println(a--);
System.out.println(a--);

System.out.println(--a);
System.out.println(--a);
System.out.println(a--);
} }
class inde
{
public static void main(String args[])
{
int i=1;
System.out.println(++i + i++ + --i + i--);
}
}

O/P:
8
class inde
{
public static void main(String args[])
{
int i=1;
System.out.println((++i) + (i++) + (--i) + (i--));
}
}

O/P:
8
class inde
{
public static void main(String args[])
{
int i=1;
System.out.println((++i) +" "+(i++) +" "+ (--i) +" "+ (i--));
int j=5;
System.out.println(--j+" "+ (++j));
}
}

O/P:
22 2 2
4 5
Statements are evaluated in following ways In java

Statement ||||||||||||||||||||||||||||||||||||||||||||||||||||||| Trace

int a= 2; a=2
int b= a++ + a++; a=2, a=3
here value of a=4
int c = ++a + a++ + a++; a=5, a=5, a=6
here value of a=7

In C/C++
Statement Trace
int a= 2; a=2
int b= a++ + a++; a=2, a=2
here value of a=4
int c = ++a + a++ + a++; a=5, a=5, a=5
here value of a=7
In short in java expression goes left to right so at the 2nd "a" it will fetch new
value and in c/c++ it will first evaluate whole expression and then increment
all operands of statement.
2.Relational Operators
• Relational operators compare two values
• They Produce a boolean value (true or false)
depending on the relationship
Operation Is true when

a >b a is greater than b


a >=b a is greater than or equal to b
a ==b a is equal to b
a !=b a is not equal to b
a <=b a is less than or equal to b
a <b a is less than b
Example

• int x = 3;
• int y = 5;
• boolean result;
result = (x > y);
• now result is assigned the value false
because 3 is not greater than 5
3. Logical Operators
Symbol Name
&& AND
|| OR
! NOT

&& T F || T F

T T F T T T

F F F F T F
Example
boolean x = true;
boolean y = false;
boolean result;

result = (x && y);


result is assigned the value false

result = ((x || y) && x);


(x || y) evaluates to true
(true && x) evaluates to true
result is then assigned the value true
4. Bitwise Operators
• Java defines several bitwise operators which can be
used with integer, long int , short ,char and byte .

• These operators act upon the individual bits of their


operands.

Category Operator Result

Bitwise Logical ~ Bitwise unary NOT


& Bitwise AND
operators | Bitwise OR
^ Bitwise exclusive OR

Bitwise shift operator >> Shift right


>>> Shift right zero fill
<< Shift left

Bitwise assignment &= Bitwise AND assignment


|= Bitwise OR assignment
operator ^= Bitwise XOR assignment
>>= Shift right assignment
>>>= Shift right zero fill assignment
<<= Shift left assignment
Bitwise Logical operators

A B A |B A &B A^B ~A
0 0 0 0 0 1

0 1 1 0 1 1

1 0 1 0 1 0

1 1 1 1 0 0
Twos Complement Numbers
Base 10 A byte of binary
+127 01111111

+4 00000100
+3 00000011
+2 00000010
+1 00000001
+0 00000000
-1 11111111
-2 11111110
-3 11111101
-4 11111100

-128 10000000
Adding Twos Complements
Base 10 Binary Base 10 Binary
+2 00000010
+3 00000011 -3 11111101
-2 11111110 -1 11111111

+1 00000001
Logical Operators (Bit Level)
& | ^ ~
int a = 10; // 00001010 = 10
int b = 12; // 00001100 = 12

a 00000000000000000000000000001010 10
& b 00000000000000000000000000001100 12
AND a & b 00000000000000000000000000001000 8

a 00000000000000000000000000001010 10
| b 00000000000000000000000000001100 12
OR a | b 00000000000000000000000000001110 14

a 00000000000000000000000000001010 10
^ b 00000000000000000000000000001100 12
XOR a ^ b 00000000000000000000000000000110 6

~ a
~a
00000000000000000000000000001010
11111111111111111111111111110101
10
-11
NOT
Logical (bit) Operator Examples
public class Example {
public static void main(String[] args) {
int a = 10; // 00001010 = 10
int b = 12; // 00001100 = 12
int and, or, xor, na;
and = a & b; // 00001000 = 8
or = a | b; // 00001110 = 14
xor = a ^ b; // 00000110 = 6
na = ~a; // 11110101 = -11
System.out.println("and " + and);
System.out.println("or " + or);
System.out.println("xor " + xor);
System.out.println("na " + na);
}
}
> java Example
and 8
or 14
xor 6
na -11
>
Shift Operators (Bit Level)
<< >> >>>

• Shift Left << Fill with Zeros

• Shift Right >> Based on Sign

• Shift Right >>> Fill with Zeros


Shift Operators << >>
int a = 3; // ...00000011 = 3
int b = -4; // ...11111100 = -4

a 00000000000000000000000000000011 3
a << 2 00000000000000000000000000001100 12
<<
Left b 11111111111111111111111111111100 -4
b << 2 11111111111111111111111111110000 -16

a 00000000000000000000000000000011 3
a >> 2 00000000000000000000000000000000 0
>>
Right b 11111111111111111111111111111100 -4
b >> 2 11111111111111111111111111111111 -1
Shift Operator >>>
int a = 3; // ...00000011 = 3
int b = -4; // ...11111100 = -4

a 00000000000000000000000000000011 3

>>> a >>> 2 00000000000000000000000000000000 0


Right 0 b 11111111111111111111111111111100 -4
b >>> 2 00111111111111111111111111111111 +big
Shift Operator Examples
public class Example {
public static void main(String[] args) {
int a = 3; // ...00000011 = 3
int b = -4; // ...11111100 = -4

System.out.println("a<<2 = " + (a<<2));


System.out.println("b<<2 = " + (b<<2));
System.out.println("a>>2 = " + (a>>2));
System.out.println("b>>2 = " + (b>>2));
System.out.println("a>>>2 = " + (a>>>2));
System.out.println("b>>>2 = " + (b>>>2));
}
}

> java Example


a<<2 = 12
b<<2 = -16
a>>2 = 0
b>>2 = -1
a>>>2 = 0
b>>>2 = 1073741823
>
The ?(ternary) Operator

• Java includes a special ternary operator


that can replace certain types of if – then
-else statements. This operator is ?.
The syntax is:
• expression1?expression2:expression3;
Hear expression1 returns boolean value.
If expression1 returns true then expression2 is
executed, if expression1 return false then expression3 is
exicuted.
Int a=10,b=20,max;
Int a=10,b=20,max;
if (a > b)
{ max = (a > b) ? a : b;
max = a;
}
Else
{
max = b;
}
• The conditional operator only works for
assigning a value to a variable.
if (a > b)
{ max = (a > b) ?
System.out.println(a); System.out.println(a) :
} System.out.println(b);
Else
{ //This may not be written like
System.out.println(b); this
} // it will produce error

You might also like