Welcome to the comprehensive guide on Java Operators! In this lesson, we'll dive deep into understanding various types of operators and their uses in Java programming. Let's get started!
Operators are special symbols that perform specific mathematical, relational, or logical operations on variables or expressions. In Java, there are several types of operators, and mastering them will help you write efficient and effective code.
Arithmetic operators are used to perform mathematical operations on numbers. Here are the basic arithmetic operators in Java:
+ : Addition- : Subtraction* : Multiplication/ : Division% : Modulus (remainder of division)++ and -- : Increment and decrement, respectively+ with operands of different types: Concatenation or type conversion (more on this later)Example 1: Basic arithmetic operations
public class ArithmeticOps {
public static void main(String[] args) {
int a = 5;
int b = 3;
int sum = a + b; // 8
int diff = a - b; // 2
int prod = a * b; // 15
float div = (float) a / b; // 1.6666667 (float division)
int mod = a % b; // 2 (remainder of division)
System.out.println("Sum: " + sum);
System.out.println("Difference: " + diff);
System.out.println("Product: " + prod);
System.out.println("Division: " + div);
System.out.println("Modulus: " + mod);
}
}Example 2: Increment and decrement operators
public class IncrementDecrement {
public static void main(String[] args) {
int num = 5;
System.out.println("Initial value: " + num); // 5
num++; // 6
System.out.println("After increment: " + num); // 6
num--; // 5
System.out.println("After decrement: " + num); // 5
}
}Relational operators compare two operands and return a boolean value (true or false). Here are the relational operators in Java:
== : Equal to!= : Not equal to< : Less than<= : Less than or equal to> : Greater than>= : Greater than or equal toExample: Using relational operators
public class RelationalOps {
public static void main(String[] args) {
int a = 5;
int b = 3;
boolean eq = a == b; // false
boolean ne = a != b; // true
boolean lt = a < b; // true
boolean le = a <= b; // true
boolean gt = a > b; // false
boolean ge = a >= b; // false
System.out.println("Equal: " + eq);
System.out.println("Not equal: " + ne);
System.out.println("Less than: " + lt);
System.out.println("Less than or equal: " + le);
System.out.println("Greater than: " + gt);
System.out.println("Greater than or equal: " + ge);
}
}Logical operators allow us to combine relational or boolean expressions and make more complex conditions. Here are the logical operators in Java:
&& : Logical AND|| : Logical OR! : Logical NOT (negation)Example: Using logical operators
public class LogicalOps {
public static void main(String[] args) {
int a = 5;
int b = 3;
boolean result = (a > 3) && (b < 5); // true (both conditions are true)
System.out.println("Result: " + result);
result = (a < 3) || (b > 5); // true (at least one condition is true)
System.out.println("Result: " + result);
boolean notResult = !result; // false (negation)
System.out.println("Not Result: " + notResult);
}
}Assignment operators are used to assign values to variables. Here are the basic assignment operators in Java:
= : Simple assignment+= : Addition and assignment-= : Subtraction and assignment*= : Multiplication and assignment/= : Division and assignment%= : Modulus and assignment&= : Bitwise AND and assignment|= : Bitwise OR and assignment^= : Bitwise XOR and assignment<<= : Bitwise left shift and assignment>>= : Bitwise right shift and assignmentExample: Using assignment operators
public class AssignmentOps {
public static void main(String[] args) {
int a = 5;
a += 3; // 8 (a = a + 3)
System.out.println("After addition assignment: " + a);
a -= 4; // 4 (a = a - 4)
System.out.println("After subtraction assignment: " + a);
}
}Question: What is the output of the following code snippet?
int a = 5;
a += 3;
System.out.println(a);A: 8
B: 5
C: 3
Correct: A
Explanation: The code adds 3 to the value of a, so the output is 8.