Operators in Java

Summarizing Java Operators

In this guide, we're going to talk more about Java operators, which are very important for anyone who programs in Java. They help us do all sorts of calculations and make decisions in our applications. Even if you already know about these operators, there's still more to learn. We'll look at both simple and complex parts of Java operators. You'll get to understand them better, which will help you write better and more efficient code. We'll cover different kinds of operators, like arithmetic and bitwise, and show you how to use them in smarter ways in your programming projects.

Arithmetic Operators

Arithmetic operators are simple tools in Java used for basic calculations.
Addition (+): Adds two numbers together. For example, the sum of 15 and 25 is 40.
Subtraction (-): Finds the difference between two numbers. For example, 30 minus 10 is 20.
Multiplication (*): Multiplies two numbers. For example, 10 times 5 is 50.
Division (/): Divides one number by another. For example, 40 divided by 8 is 5.
Modulus (%): Calculates the remainder of a division. For example, the remainder of 13 divided by 5 is 3."

Assignment Operators

Assignment operators in Java are used to assign values to variables in different ways.
Simple Assignment (=): Assigns the right-hand value to the left-hand variable.
Addition Assignment (+=): Adds the right value to the left variable and updates the left variable.
Subtraction Assignment (-=): Subtracts the right value from the left variable and updates the left variable.
Multiplication Assignment (*=): Multiplies the left variable by the right value and updates the left variable.
Division Assignment (/=): Divides the left variable by the right value and updates the left variable.
Modulus Assignment (%=): Divides the left variable by the right value and assigns the remainder to the left variable.
Here is a consolidated example demonstrating these operators:
public class AssignmentOperatorsExample {
public static void main(String[] args) {
// Simple Assignment
int a = 15;
char ch = 'z';
System.out.println("a is assigned: " + a); // Output: a is assigned: 15
System.out.println("ch is assigned: " + ch); // Output: ch is assigned: z

// Addition Assignment
int b = 10;
b += 5;
System.out.println("b after addition and assignment: " + b); // Output: b after addition and assignment: 15

// Subtraction Assignment
int c = 20;
c -= 5;
System.out.println("c after subtraction and assignment: " + c); // Output: c after subtraction and assignment: 15

// Multiplication Assignment
int d = 3;
d *= 4;
System.out.println("d after multiplication and assignment: " + d); // Output: d after multiplication and assignment: 12

// Division Assignment
int e = 20;
e /= 4;
System.out.println("e after division and assignment: " + e); // Output: e after division and assignment: 5

// Modulus Assignment
int f = 11;
f %= 4;
System.out.println("f after modulus and assignment: " + f); // Output: f after modulus and assignment: 3
}
}

Unary Operators

Unary operators in Java are used to modify the values of variables directly. They come in four main types:
Post-Increment (num++): Increases num by 1 after the current value is used. For example, if num is initially 5, num++ uses the value 5, then increases it to 6.
Pre-Increment (++num): Increases num by 1 before using it in any expression. For example, if num is initially 5, ++num immediately changes it to 6 and then uses the new value.
Post-Decrement (num--): Decreases num by 1 after the current value is used. For example, if num is 5, num-- uses the value 5, then decreases it to 4
Pre-Decrement (--num): Decreases num by 1 before using it in any expression. For example, if num is 5, --num immediately changes it to 4 and then uses the new value.
Here's a consolidated example demonstrating these operators:
public class UnaryOperatorsExample {
public static void main(String[] args) {
int num = 5;

// Post-Increment
System.out.println("Before post-increment: " + num); // Output: 5
System.out.println("Using post-increment: " + num++); // Uses 5, then increments
System.out.println("After post-increment: " + num); // Output: 6

// Pre-Increment
System.out.println("Using pre-increment: " + ++num); // Increments first, uses 7
System.out.println("After pre-increment: " + num); // Output: 7

// Post-Decrement
System.out.println("Before post-decrement: " + num); // Output: 7
System.out.println("Using post-decrement: " + num--); // Uses 7, then decrements
System.out.println("After post-decrement: " + num); // Output: 6

// Pre-Decrement
System.out.println("Using pre-decrement: " + --num); // Decrements first, uses 5
System.out.println("After pre-decrement: " + num); // Output: 5
}
}

Comparison Operators

Comparison operators let you compare two values and determine the relationship between them. Here's a quick guide:
Equal to (==): Checks if two values are the same. For example, checking if 10 is equal to 10 returns true.
Not Equal to (!=): Determines if two values are different. For instance, checking if 10 is not equal to 20 returns true.
Greater than (>): Checks if the value on the left is larger than the one on the right. Checking if 15 is greater than 10 returns true.
Less than (<): Checks if the value on the left is smaller than the one on the right. For example, checking if 8 is less than 10 returns true.
Greater than or equal to (>=): Determines if the value on the left is larger than or equal to the one on the right. Checking if 10 is greater than or equal to 10 returns true.
Less than or equal to (<=): Determines if the value on the left is smaller than or equal to the one on the right. Checking if 5 is less than or equal to 10 returns true.
Here is a consolidated example demonstrating these operators:
class ComparisonOperatorsExample {
public static void main(String[] args) {
// Variables for comparison
int a = 10, b = 10;
int c = 20;
int d = 15;
int e = 8;
int f = 5;

// Equal to (==)
System.out.println("Is a equal to b? " + (a == b)); // Output: Is a equal to b? true

// Not Equal to (!=)
System.out.println("Is a not equal to c? " + (a != c)); // Output: Is a not equal to c? true

// Greater than (>)
System.out.println("Is d greater than b? " + (d > b)); // Output: Is d greater than b? true

// Less than (<)
System.out.println("Is e less than b? " + (e < b)); // Output: Is e less than b? true

// Greater than or equal to (>=)
System.out.println("Is a greater than or equal to b? " + (a >= b)); // Output: Is a greater than or equal to b? true

// Less than or equal to (<=)
System.out.println("Is f less than or equal to b? " + (f <= b)); // Output: Is f less than or equal to b? true
}
}

Logical Operators

Logical operators are used to combine multiple conditions or invert the state of a condition in Java programming.
AND (&&): This operator returns true only if both operands (conditions) are true. If either condition is false, it returns false. For example, in the expression (x > 0 && y > 0), both x and y must be greater than zero for the result to be true.
OR (||): This operator returns true if at least one of the operands is true. It returns false only if both operands are false. For example, (x > 0 || y > 0) returns true if either x or y (or both) is greater than zero.
NOT (!): This operator reverses the logical state of its operand. If a condition is true, the NOT operator will make it false, and vice versa. For example, !true becomes false, and !(x > 0) becomes true if x is not greater than zero.
Here is a consolidated example demonstrating these operators:
public class LogicalOperatorsExample {
public static void main(String[] args) {
int x = 5;
int y = 10;
int z = -5;

// Example of AND (&&) operator
if (x > 0 && y > 0) {
System.out.println("Both x and y are greater than zero."); // This will be printed
}

// Example of OR (||) operator
if (x > 0 || z > 0) {
System.out.println("At least one of x or z is greater than zero."); // This will be printed
}

// Example of NOT (!) operator
boolean condition = true;
if (!condition) {
System.out.println("Condition is false.");
} else {
System.out.println("Condition is true."); // This will be printed
}
}
}

Bitwise Operators

Bitwise operators in Java allow you to manipulate individual bits of integer values directly.
AND (&): Combines bits of two numbers. A bit in the result is 1 only if both corresponding bits are 1, otherwise it's 0.
OR (|): Combines bits where at least one of the corresponding bits is 1, resulting in 1; otherwise, it's 0.
XOR (^): Results in 1 only if the corresponding bits of the operands are different.
NOT (~): Inverts all the bits of a number.
Left Shift (<<): Moves the bits to the left, dropping the left-most bit and filling the right-most bit with zeros.
Right Shift (>>): Moves the bits to the right. The left-most bits are filled based on the sign bit of the original number (sign extension).
Here is a consolidated example demonstrating these operators:
public class BitwiseOperatorsExample {
public static void main(String[] args) {
int a = 12, b = 6, c = 5, d = 20;

// AND operation
System.out.println("a & b = " + (a & b)); // Output: 4 (0100 in binary)

// OR operation
System.out.println("a | b = " + (a | b)); // Output: 14 (1110 in binary)

// XOR operation
System.out.println("a ^ b = " + (a ^ b)); // Output: 10 (1010 in binary)

// NOT operation
System.out.println("~c = " + (~c)); // Output: -6 (inverts all bits of 5)

// Left Shift operation
System.out.println("c << 2 = " + (c << 2)); // Output: 20 (10100 in binary)

// Right Shift operation
System.out.println("d >> 2 = " + (d >> 2)); // Output: 5 (0101 in binary)
}
}

Conclusion

In this guide, we looked closely at the different operators you can use in Java. We saw how each one can make your programming better. Assignment operators make your code more efficient, while comparison and logical operators give you more control. Bitwise operators let you work at a low level. Knowing these operators well is key to writing strong and efficient Java code.
Try using these operators in various situations to see how they work. This will help you find new ways to make your code better and more efficient. Remember, it's important to not only know what each operator does but also when and how to use them well in your coding.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.