Java Module 1 Chapter 4
Java 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;
• 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.
● 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.
● It is hard to remember the operator precedence because there are too many operators