- An operator is a symbol that the compiler to perform a specific operation on operands.
- Example : a + b = c
- In the above example, 'a' and 'b' are operands on which the '+' operator is applied.
- Arithmetic operators are used to perform mathematical operations such as addition, division, etc on expressions.
- Arithmetic operators cannot work with Booleans.
- % operator can work on floats and doubles.
- Let x=7 and y=2
- As the name suggests, these operators are used to compare two operands.
- Let x=7 and y=2
- These operators determine the logic in an expression containing two or more values or variables.
- Let x = 8 and y =2
- These operators perform the operations on every bit of a number.
- Let x =2 and y=3. So 2 in binary is 100, and 3 is 011.
- The operators are applied and evaluated based on precedence. For example, (+, -) has less precedence compared to (, /). Hence '' and / are evaluated first.
- In case we like to change this order, we use parenthesis ()
package com.company;
public class CWH_Ch2_Operators {
public static void main(String[] args) {
// 1. Arithmetic Operators
int a = 4;
// int b = 6 % a; // Modulo Operator
// 4.8%1.1 --> Returns Decimal Remainder
// 2. Assignment Operators
int b = 9;
b *= 3;
System.out.println(b);
// 3. Comparison Operators
// System.out.println(64<6);
// 4. Logical Operators
// System.out.println(64>5 && 64>98);
System.out.println(64>5 || 64>98);
// 5. Bitwise Operators
System.out.println(2&3);
// 10
// 11
// ----
// 10
}
}