Introduce Java Operators and give examples
Java operators are special symbols that perform operations on variables and values. They can be categorized into several types based on their functionality. Here’s an overview of the main types of operators in Java, along with examples for each:
These operators are used to perform basic mathematical operations.
+
): Adds two operands.-
): Subtracts the second operand from the first.*
): Multiplies two operands./
): Divides the numerator by the denominator.%
): Returns the remainder of a division operation.Example:
int a = 10;
int b = 3;
System.out.println("Addition: " + (a + b)); // 13
System.out.println("Subtraction: " + (a - b)); // 7
System.out.println("Multiplication: " + (a * b)); // 30
System.out.println("Division: " + (a / b)); // 3
System.out.println("Modulus: " + (a % b)); // 1
These operators are used to compare two values.
==
): Checks if two operands are equal.!=
): Checks if two operands are not equal.>
): Checks if the left operand is greater than the right.<
): Checks if the left operand is less than the right.>=
): Checks if the left operand is greater than or equal to the right.<=
): Checks if the left operand is less than or equal to the right.Example:
int x = 5;
int y = 10;
System.out.println("x == y: " + (x == y)); // false
System.out.println("x != y: " + (x != y)); // true
System.out.println("x > y: " + (x > y)); // false
System.out.println("x < y: " + (x < y)); // true
System.out.println("x >= y: " + (x >= y)); // false
System.out.println("x <= y: " + (x <= y)); // true
These operators are used to combine multiple boolean expressions.
&&
): Returns true if both operands are true.||
): Returns true if at least one of the operands is true.!
): Reverses the logical state of its operand.Example:
boolean a1 = true;
boolean a2 = false;
System.out.println("a1 && a2: " + (a1 && a2)); // false
System.out.println("a1 || a2: " + (a1 || a2)); // true
System.out.println("!a1: " + (!a1)); // false
These operators perform operations on bits and are used for bit manipulation.
&
): Performs a bitwise AND operation.|
): Performs a bitwise OR operation.^
): Performs a bitwise exclusive OR operation.~
): Inverts all the bits.<<
): Shifts bits to the left, filling with zeros.>>
): Shifts bits to the right.Example:
int p = 5; // 0101 in binary
int q = 3; // 0011 in binary
System.out.println("p & q: " + (p & q)); // 1 (0001)
System.out.println("p | q: " + (p | q)); // 7 (0111)
System.out.println("p ^ q: " + (p ^ q)); // 6 (0110)
System.out.println("~p: " + (~p)); // -6 (inverts bits)
System.out.println("p << 1: " + (p << 1)); // 10 (1010)
System.out.println("p >> 1: " + (p >> 1)); // 2 (0010)
These operators are used to assign values to variables.
=
): Assigns the right operand to the left operand.+=
): Adds the right operand to the left operand and assigns the result.-=
): Subtracts the right operand from the left operand and assigns the result.*=
): Multiplies the left operand by the right operand and assigns the result./=
): Divides the left operand by the right operand and assigns the result.%=
): Takes the modulus using two operands and assigns the result.Example:
int num = 10;
num += 5; // num = num + 5
System.out.println("After += : " + num); // 15
num -= 3; // num = num - 3
System.out.println("After -= : " + num); // 12
num *= 2; // num = num * 2
System.out.println("After *= : " + num); // 24
num /= 4; // num = num / 4
System.out.println("After /= : " + num); // 6
num %= 5; // num = num % 5
System.out.println("After %= : " + num); // 1
These operators operate on a single operand.
+
): Indicates a positive value (usually optional).-
): Negates the value.++
): Increases the value by 1.--
): Decreases the value by 1.Example:
int value = 10;
System.out.println("Unary plus: " + (+value)); // 10
System.out.println("Unary minus: " + (-value)); // -10
value++; // Increment
System.out.println("After increment: " + value); // 11
value--; // Decrement
System.out.println("After decrement: " + value); // 10
The ternary operator is a shorthand for the if-else
statement.
Syntax: condition ? expression1 : expression2
Example:
int a = 5;
int b = 10;
int max = (a > b) ? a : b; // If a is greater than b, max is a; otherwise, max is b
System.out.println("Max value: " + max); // 10
Java operators are essential for performing various operations in programming. Understanding how to use these operators effectively is crucial for writing efficient and functional Java code.