Module 1 – Chapter 4
OPERATORS
Arithmetic Operators
Bitwise Operators
Relational Operators
Boolean Logical Operators
Assignment Operator
Unary Operators
? Operator
Operators are special symbols that perform specific operations on one,
two, or sometimes three operands and return a result.
Table of Operators
What is the output?
class Operator {
public static void main(String args[]) {
int a = 1 + 1;
int b = a * 3;
int c = b / 4;
int d = c - a;
int e = -d;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
System.out.println("e = " + e);
Continued…
System.out.println("\nFloating Point Arithmetic");
double da = 1 + 1;
double db = da * 3;
double dc = db / 4;
double dd = dc - da;
double de = -dd;
System.out.println("da = " + da);
System.out.println("db = " + db);
System.out.println("dc = " + dc);
System.out.println("dd = " + dd);
System.out.println("de = " + de);
}
}
Modulus operator
● It returns the remainder of a division operation.
● Applied to integer and floating point.
Modulus operator example
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);
}
} What is the practical use of %?
Lab Activity/Exercise 8
Using the modulus operator write a program that prints the first 100
positive odd integers (each integer should be separated from the next one
by a comma, except the last – 1,3,5,…, 100th odd integer) and then print
on a separate line the first 100 positive even integers (each integer should
be separated from the next one by a comma, except the last – 0, 2, 4,…,
100th even integer).
Arithmetic compound assignment operators
int a = -1;
a = a + 4; this could also be written as a += 4;
+= considered as compound assignment operator.
In general, variable = variable operator expression; can be rewritten as
variable operator=expression;
// Demonstrating several assignment operators.
class OperatorEquals {
public static void main(String args[]) {
int a = 1;
int b = 2;
int c = 3;
a += 5;
b *= 4;
c += a * b;
c %= 6;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
}
}
Increment and decrement
● ++ and the – – are Java’s increment and decrement operators.
● The increment operator increases its operand by one. The decrement operator decreases its operand by one.
● x = x + 1; can be written as x++;
● x = x-1; can be written as x--;
● The operand is incremented or decremented before the value is obtained for use in the expression.
● x = 42;
● y = ++x;
● y is set to 43
● The previous value is obtained for use in the expression.
● x = 42;
● y = x++;
● x is obtained before the increment operator is executed, so the value of y is 42, in both cases x is set to 43.
// Demonstrate ++.
class IncrementDecrement {
public static void main(String args[]) {
int a = 1;
int b = 2;
int c;
int d;
c = ++b;
d = a++;
c++;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
}
}
The Bitwise Operators
Binary representation
●Negative numbers are obtained by inverting all the bits and adding 1 to
the inverted number.
42 = 00101010 – write the positive value with the sign bit as 0
-42 = 11010101 – get its 1’s complement
1 – finally add one
11010110
● To decode it,
-42 = 11010110 (take its one’s complement)
= 00101001 (add 1)
1
00101010
The Bitwise Logical Operators
The bitwise logical operators are &, |, ^, and ~
●
Bitwise AND
00101010 42
&
00001111 15
00001010 10
Bitwise OR
00101010 42
00001111 15
00101111 47
Bitwise XOR
00101010 42
^
00001111 15
00100101 37
The Left Shift
●left shift operator, << shifts all of the bits in a value to the left a specified
number of times.
● value << num
● each left shift has the effect of doubling the original value
// Left shifting as a quick way to multiply by 2.
class MultByTwo {
public static void main(String args[]) {
int num = 00001010
for (int i = 0; i < 2; i++) {
num = num << 1;
System.out.println(num);
}
}
} // what is the output?
The Right Shift
• The right shift operator (>>) shifts all of the bits in a value to
the right a specified number of Times.
• value >> num
• Each time you shift a value to the right, it divides that value
by two
Relational Operators
● The relational operators determine the relationship that one
operand has to the other.
● They determine equality and order.
● the outcome of these operations is a boolean value.
● int a = 4;
● int b = 1;
● boolean c = a < b;
Operator Result
== Equal to
!= Not equal to
> Greater Than
< Less than
>= Greater Than or equal to
<= Less than or Equal to
●if (done == 0) in C and done is an integer variable => if (!done) in Java
and done is a boolean variable
●if (done != 0) in C and done is an integer variable => if(done) in Java
and done is a Boolean variable
Boolean Logical Operators
●All of the binary logical operators combine two boolean values to form a
resultant boolean value.
Operator and result -table
Table of boolean logical values
class BoolLogic {
public static void main(String args[]) {
boolean a = true;
boolean b = false;
boolean c = a | b;
boolean d = a & b;
boolean e = a ^ b;
boolean f = (!a & b) | (a & !b);
boolean g = !a;
System.out.println(" a = " + a);
System.out.println(" b = " + b);
System.out.println(" a|b = " + c);
System.out.println(" a&b = " + d);
System.out.println(" a^b = " + e);
System.out.println("!a&b|a&!b = " + f);
System.out.println(" !a = " + g);
}
Short-Circuit Logical Operators
● &&, ||
The Assignment Operator
● var = expression;
● int x, y, z;
● x = y = z = 100; // set x, y, and z to 100
● “chain of assignment”
The ? Operator
● It is called a Ternary Operator.
● Can replace certain types of if-then-else statements.
● Syntax: expression1 ? expression2 : expression3
Operator Precedence
●Using Parentheses: Parentheses raise the precedence of the operations that are inside
them.
● Consider an expression a >> b + 3;
● How do we know if this is interpreted as either
● a >> (b + 3); or
● (a >> b) + 3
● It is hard to remember the operator precedence because there are too many operators
● Use parentheses instead. For example a >> (b+3);
● It solves the ambiguity.