Lecture 3 Operators in Java
Lecture 3 Operators in Java
IN2203
Fundamentals of
Programming (Java)
Instructor
Dr. Muhammad Waqar
muhammad.aslam@purescollege.ca
An Overview of Lecture 2
We discussed the following in lecture2
Tests
• Primitive Data types, byte, short, int, long, float, double, char,
boolean, String
• Declaration of variables, variable scope
• Escape Sequences
• Type Promotion and Casting
1
6/13/2020
• Different kind of packages are available for getting input from user including
Bufferreader class, Scanner class and Console class.
• Whichever class you use you will have to first import its package and then
will have to instantiate an object to use methods of that class.
• An example is given on next page which used Scanner class to get input from
user and then displays input using println.
2
6/13/2020
Practice Program
Write a program which asks user to enter radius. Then it
calculates the area of circle using radius and prints the area
Tests
on screen. The output of program should be as shown
below.
Tests
public static void main(String args[]) {
3
6/13/2020
Operators
• Operators are used to apply some operation on variables and values.
• The value or variables are called operands, while the operation (to be
performed between the two operands) isTests
defined by an operator
• Java provides a rich operator environment.
• Most of its operators can be divided into the following four groups:
arithmetic, bitwise, relational, and logical.
• Java also defines some additional operators that handle certain special
situations.
Arithmetic Operators
• The operands of the arithmetic operators must be of a numeric type. You
cannot use them on boolean types, but you can use them on char types,
since the char type in Java is, essentially,Tests
a subset of int.
4
6/13/2020
10
3- int i = 10;
int j = 5;
i = i / j;
10
5
6/13/2020
11
Operators Precedence
Tests
11
12
Arithmetic Overflow
What is the final value stored in i in this program? Is this what you expected?
Tests
class Test {
// A program to demonstrate integer overflow
12
6
6/13/2020
13
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.
Tests
// 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 = " + (y % 10));
}
}
13
14
14
7
6/13/2020
15
15
16
16
8
6/13/2020
17
17
18
18
9
6/13/2020
19
19
20
20
10
6/13/2020
21
• 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.
21
22
byte x = 0b00101010;
byte y = (byte) ~x;
System.out.println("x = " + x);
System.out.println("y = " + y);
}
}
22
11
6/13/2020
23
00101010 42
& 00001111 15
00001010 10
class BitwiseAND {
public static void main (String[] args) {
int number1 = 42, number2 = 15, result;
result = number1 & number2;
System.out.println(result);}
}
23
24
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:
Tests
00101010 42
| 00001111 15
00101111 47
class BitwiseOR {
public static void main(String[] args) {
int number1 = 42, number2 = 15, result;
result = number1 | number2;
System.out.println(result);}
}
24
12
6/13/2020
25
00101010 42
^ 00001111 15
00100101 37
Write a program which uses inverting bit property of bitwise xor to take one’s
complement.
25
26
byte x = 0b00101010;
byte y = (byte) (x^0b11111111);
byte z = (byte) (y + 1);
System.out.println("x = " + x);
System.out.println("y = " + y);
System.out.println("z = " + z);
}
}
26
13
6/13/2020
27
• Here, num specifies the number of positions to left-shift the value in value
• For each shift left, the high-order bit is shifted out (and lost), and a zero is
brought in on the right.
• This means that when a left shift is applied to an int operand, bits are lost
once they are shifted past bit position 31. If the operand is a long, then bits
are lost after bit position 63.
• Java’s automatic type promotions produce unexpected results when you are
shifting byte and short values. In order to use shift on byte, you have to do
casting to byte again after operation.
27
28
byte a = 64, b;
int i;
i = a << 2;
b = (byte) (a << 2);
System.out.println("Original value of a: " + a);
System.out.println("i and b: " + i + " " + b);
}
}
28
14
6/13/2020
29
class MultByTwo {
public static void main(String args[]) {
byte num = 32;
num = (byte) (num << 1);
System.out.println(num);
num = (byte) (num << 1);
System.out.println(num);}
}}}
29
30
30
15
6/13/2020
31
int a = -1;
a = a >>> 24;
Here is the same operation in binary form to further illustrate what is
happening:
11111111 11111111 11111111 11111111 –1 in binary as an int
>>>24
00000000 00000000 00000000 11111111 255 in binary as an int
31
32
32
16
6/13/2020
Thank You
33
17