Week-5-Logical-Arithmetic-and-Relational-Operators
Week-5-Logical-Arithmetic-and-Relational-Operators
Java Operators
Operators are symbols that perform operations on variables and values. For example, + is an operator used
for addition, while * is also an operator used for multiplication. Operators in Java can be classified into 5
types:
1. Arithmetic Operators
2. Assignment Operators
3. Relational Operators
4. Logical Operators
a + b;
Here, the + operator is used to add two variables a and b. Similarly, there are various other arithmetic
operators in Java.
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
// declare variables
int a = 12, b = 5;
// addition operator
System.out.println("a + b = " + (a + b));
// subtraction operator
System.out.println("a - b = " + (a - b));
// multiplication operator
System.out.println("a * b = " + (a * b));
// division operator
System.out.println("a / b = " + (a / b));
// modulo operator
Output
a + b = 17
a-b=7
a * b = 60
a/b=2
a%b=2
In the above example, we have used +, -, and * operators to compute addition, subtraction, and multiplication
operations.
/ Division Operator
Note the operation, a / b in our program. The / operator is the division operator.
If we use the division operator with two integers, then the resulting quotient will also be an integer. And, if
one of the operands is a floating-point number, we will get the result will also be in floating-point.
In Java,
(9 / 2) is 4
(9.0 / 2) is 4.5
(9 / 2.0) is 4.5
(9.0 / 2.0) is 4.5
% Modulo Operator
The modulo operator % computes the remainder. When a = 7 is divided by b = 4, the remainder is 3.
int age;
age = 5;
Here, = is the assignment operator. It assigns the value on its right to the variable on its left. That is, 5 is
assigned to the variable age.
Let's see some more assignment operators available in Java.
Operator Example Equivalent to
= a = b; a = b;
+= a += b; a = a + b;
-= a -= b; a = a - b;
/= a /= b; a = a / b;
%= a %= b; a = a % b;
Example 2: Assignment Operators
class Main {
public static void main(String[] args) {
// create variables
int a = 4;
int var;
Output
var using =: 4
var using +=: 8
var using *=: 32
Here, < operator is the relational operator. It checks if a is less than b or not. It returns either true or false.
Operator Description Example
// create variables
int a = 7, b = 11;
// value of a and b
System.out.println("a is " + a + " and b is " + b);
// == operator
System.out.println(a == b); // false
// != operator
System.out.println(a != b); // true
// > operator
System.out.println(a > b); // false
// < operator
System.out.println(a < b); // true
// >= operator
System.out.println(a >= b); // false
// <= operator
System.out.println(a <= b); // true
}
}
&& (Logical expression1 && true only if both expression1 and expression2
AND) expression2 are true
// && operator
System.out.println((5 > 3) && (8 > 5)); // true
System.out.println((5 > 3) && (8 < 5)); // false
// || operator
System.out.println((5 < 3) || (8 > 5)); // true
System.out.println((5 > 3) || (8 < 5)); // true
System.out.println((5 < 3) || (8 < 5)); // false
// ! operator
System.out.println(!(5 == 3)); // true
System.out.println(!(5 > 3)); // false
}
Working of Program
(5 > 3) && (8 > 5) returns true because both (5 > 3) and (8 > 5) are true.
(5 > 3) && (8 < 5) returns false because the expression (8 < 5) is false.
(5 < 3) || (8 > 5) returns true because the expression (8 > 5) is true.
(5 > 3) || (8 < 5) returns true because the expression (5 > 3) is true.
(5 < 3) || (8 < 5) returns false because both (5 < 3) and (8 < 5) are false.
!(5 == 3) returns true because 5 == 3 is false.
!(5 > 3) returns false because 5 > 3 is true.
int myInt = 12 - 4 * 2;
What will be the value of myInt? Will it be (12 - 4)*2, that is, 16? Or it will be 12 - (4 * 2), that is, 4?
When two operators share a common operand, 4 in this case, the operator with the highest precedence is
operated first. In Java, the precedence of * is higher than that of -. Hence, the multiplication is performed
before subtraction, and the value of myInt will be 4.
References:
https://www.programiz.com/java-programming/operators