Operators
Operators
In Java, operators are special symbols used to perform operations on variables and values.
They are used to perform calculations, comparisons, and logical decisions in a program.
1. Arithmetic Operators
2. Assignment Operators
3. Comparison Operators
4. Logical Operators
5. Bitwise Operators
Arithmetic Operators
Arithmetic operators are used in Java to do basic math operations on numbers.
1. Addition (+)→The addition operator is used to add two values.
2. Subtraction (-)→The subtraction operator is used to subtract one value from another.
3. Multiplication (*)→The multiplication operator is used to multiply two values.
4. Division (/)→The division operator is used to divide one number by another.
5. Modulus (%)→The modulus operator is used to find the remainder after division.
1. Increment Operator (++)→The increment operator (++) increases the value of a variable by 1.
- It is a shortcut for: x = x + 1
- It can be used in two ways:
- Pre-increment (++x) → increases value first, then uses it
- Post-increment (x++) → uses value first, then increases it
2. Decrement Operator (--)→The decrement operator (--) decreases the value of a variable by 1.
- It is a shortcut for: x = x - 1
- It also has two forms:
- Pre-decrement (--x) → decreases value first, then uses it
- Post-decrement (x--) → uses value first, then decreases it
Example
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 5;
// Addition (+) : adds two numbers
System.out.println("Addition: " + (a + b));
// Subtraction (-) : subtracts second number from first
System.out.println("Subtraction: " + (a - b));
// Multiplication (*) : multiplies two numbers
System.out.println("Multiplication: " + (a * b));
// Division (/) : divides first number by second
System.out.println("Division: " + (a / b));
// Modulus (%) : gives remainder after division
System.out.println("Modulus: " + (a % b));
// Increment (++) : increases value by 1
System.out.println("Increment (a++): " + (a++));
// After increment
System.out.println("After Increment: " + a);
// Decrement (--) : decreases value by 1
System.out.println("Decrement (b--): " + (b--));
// After decrement
System.out.println("After Decrement: " + b);
}
}
Assignment operators
Assignment operators are symbols in Java used to give or update values of variables.
- = → Assigns value
- += → Adds and assigns
- -= → Subtracts and assigns
- *= → Multiplies and assigns
- /= → Divides and assigns
- %= → Finds remainder and assigns
Example
public class Main {
public static void main(String[] args) {
int a = 10;
// = (Assignment): assigns value to variable
int b = a;
System.out.println("Assign (=): " + b);
// += (Add and assign): adds and updates value
b += 5; // b = b + 5
System.out.println("Add and Assign (+=): " + b);
// -= (Subtract and assign): subtracts and updates value
b -= 3; // b = b - 3
System.out.println("Subtract and Assign (-=): " + b);
// *= (Multiply and assign): multiplies and updates value
b *= 2; // b = b * 2
System.out.println("Multiply and Assign (*=): " + b);
// /= (Divide and assign): divides and updates value
b /= 2; // b = b / 2
System.out.println("Divide and Assign (/=): " + b);
// %= (Modulus and assign): finds remainder and updates value
b %= 3; // b = b % 3
System.out.println("Modulus and Assign (%=): " + b);
}
}
Comparison operators
Comparison operators are used in Java to compare two values and give the result as true or false
- == → Equal to
- != → Not equal to
- > → Greater than
- < → Less than
- >= → Greater than or equal to
- <= → Less than or equal to
Example
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 5;
// == (Equal to): checks if both values are equal
System.out.println("Equal to (==): " + (a == b));
// != (Not equal to): checks if values are not equal
System.out.println("Not equal to (!=): " + (a != b));
// > (Greater than): checks if a is greater than b
System.out.println("Greater than (>): " + (a > b));
// < (Less than): checks if a is less than b
System.out.println("Less than (<): " + (a < b));
// >= (Greater than or equal to): checks condition
System.out.println("Greater than or equal to (>=): " + (a >= b));
// <= (Less than or equal to): checks condition
System.out.println("Less than or equal to (<=): " + (a <= b));
}
}
Logical Operators
Logical operators in Java are used to combine two or more conditions and return a result as true or false.
- && → Logical AND (true if all conditions are true)
- || → Logical OR (true if at least one condition is true)
- ! → Logical NOT (reverses the result)
Example
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 5;
// == (Equal to): checks if both values are equal
System.out.println("Equal to (==): " + (a == b));
// != (Not equal to): checks if values are not equal
System.out.println("Not equal to (!=): " + (a != b));
// > (Greater than): checks if a is greater than b
System.out.println("Greater than (>): " + (a > b));
// < (Less than): checks if a is less than b
System.out.println("Less than (<): " + (a < b));
// >= (Greater than or equal to): checks condition
System.out.println("Greater than or equal to (>=): " + (a >= b));
// <= (Less than or equal to): checks condition
System.out.println("Less than or equal to (<=): " + (a <= b));
}
}
Bitwise Operators
Bitwise operators are used in Java to perform operations on the binary (bit-level) representation of numbers.
- & → Bitwise AND(Returns 1 if both bits are 1, otherwise returns 0.)
- | → Bitwise OR(Returns 1 if at least one bit is 1.)
- ^ → Bitwise XOR(Returns 1 if both bits are different.)
- ~ → Bitwise NOT(Reverses all bits (0 becomes 1 and 1 becomes 0).)
- << → Left shift(Shifts bits to the left and adds zeros on the right.)
- >> → Right shift(Shifts bits to the right and removes bits from the right side.)
Example
public class Main {
public static void main(String[] args) {
int a = 5; // binary: 0101
int b = 3; // binary: 0011
// & (Bitwise AND): returns 1 if both bits are 1
System.out.println("AND (&): " + (a & b));
// | (Bitwise OR): returns 1 if at least one bit is 1
System.out.println("OR (|): " + (a | b));
// ^ (Bitwise XOR): returns 1 if bits are different
System.out.println("XOR (^): " + (a ^ b));
// ~ (Bitwise NOT): inverts all bits
System.out.println("NOT (~): " + (~a));
// << (Left shift): shifts bits to the left (multiply by 2)
System.out.println("Left Shift (<<): " + (a << 1));
// >> (Right shift): shifts bits to the right (divide by 2)
System.out.println("Right Shift (>>): " + (a >> 1));
}
}