Arithmetic operators
Name | Operators |
Addition | + |
Subtraction | - |
Multiplication | * |
Division | / |
Modulus | % |
public class ArithmeticOperatorsExample {public static void main(String[] args) {// Additionint sum = 5 + 3;System.out.println("Sum: " + sum); // Output: Sum: 8// Subtractionint difference = 7 - 4;System.out.println("Difference: " + difference); // Output: Difference: 3// Multiplicationint product = 6 * 2;System.out.println("Product: " + product); // Output: Product: 12// Divisiondouble quotient = 10.0 / 3;System.out.println("Quotient: " + quotient); // Output: Quotient: 3.3333333333333335// Modulus (Remainder)int remainder = 10 % 3;System.out.println("Remainder: " + remainder); // Output: Remainder: 1
Comparison operators in Java are used to compare two values and determine the relationship between them.
The Java comparison operators are:
Relational Operators
Name | Operators |
Equal to | == |
Not equal to | != |
Less than | < |
Greater than | > |
Less than or equal to | <= |
Greater than or equal to | >= |
public class RelationalOperatorsExample {public static void main(String[] args) {int a = 5, b = 7;System.out.println("a == b: " + (a == b)); // falseSystem.out.println("a != b: " + (a != b)); // trueSystem.out.println("a > b: " + (a > b)); // falseSystem.out.println("a < b: " + (a < b)); // trueSystem.out.println("a >= b: " + (a >= b)); // falseSystem.out.println("a <= b: " + (a <= b)); // true}}
Logical Operators
A summary of each is as follows:
Name | Operators |
Logical AND | && |
Logical OR | || |
Logical NOT | ! |
public class LogicalOperatorsExample {public static void main(String[] args) {boolean isSunny = true;boolean isWarm = false;// Logical ANDboolean goForWalk = isSunny && isWarm;System.out.println("Go for a walk: " + goForWalk); // false// Logical ORboolean goForPicnic = isSunny || isWarm;System.out.println("Go for a picnic: " + goForPicnic); // true// Logical NOTboolean stayInside = !goForPicnic;System.out.println("Stay inside: " + stayInside); // false}}
Assignment Operators
Assignment operators in Java are used to assign values to variables.Name | Operators |
Assign | = |
Add and assign | += |
Subtract and assign | -= |
Multiply and assign | *= |
Divide and assign | /= |
Modulus and assign | %= |
public class AssignmentOperatorsExample {public static void main(String[] args) {int x = 5;// Addition Assignmentx += 3; // x is now 8// Subtraction Assignmentx -= 2; // x is now 6// Multiplication Assignmentx *= 4; // x is now 24// Division Assignmentx /= 3; // x is now 8// Modulus Assignmentx %= 5; // x is now 3System.out.println("Final value of x: " + x); // Output: Final value of x: 3}}
Increment and Decrement Operators
Java's increment and decrement operators are unary operators used to increase or decrease the value of a variable by 1, respectively.Name | Operators |
Increment | ++ |
Decrement | -- |
public class IncrementDecrementOperatorsExample {public static void main(String[] args) {int counter = 5;// Postfix Incrementint result1 = counter++;System.out.println("Postfix Increment - Result: " + result1 + ", Counter: " + counter);// Prefix Incrementint result2 = ++counter;System.out.println("Prefix Increment - Result: " + result2 + ", Counter: " + counter);// Postfix Decrementint result3 = counter--;System.out.println("Postfix Decrement - Result: " + result3 + ", Counter: " + counter);// Prefix Decrementint result4 = --counter;System.out.println("Prefix Decrement - Result: " + result4 + ", Counter: " + counter);}}
Bitwise operators in Java operate on individual bits of an integer value.
These operators perform bit-level operations and are often used in low-level programming, such as manipulating hardware or implementing certain algorithms.
Bitwise operators in Java are:
Bitwise Operators
Name | Operators |
Bitwise AND | & |
Bitwise OR | | |
Bitwise XOR | ^ |
Bitwise NOT | ~ |
Left shift | << |
Right shift | >> |
Unsigned right shift | >>> |
public class BitwiseOperatorsExample {public static void main(String[] args) {int a = 5;int b = 3;// Bitwise ANDint resultAnd = a & b;System.out.println("Bitwise AND: " + resultAnd); // Output: 1// Bitwise ORint resultOr = a | b;System.out.println("Bitwise OR: " + resultOr); // Output: 7// Bitwise XORint resultXor = a ^ b;System.out.println("Bitwise XOR: " + resultXor); // Output: 6// Bitwise NOTint num = 5;int resultNot = ~num;System.out.println("Bitwise NOT: " + resultNot); // Output: -6// Left Shiftint value = 5;int resultLeftShift = value << 2;System.out.println("Left Shift: " + resultLeftShift); // Output: 20// Right Shiftint number = -16;int resultRightShift = number >> 2;System.out.println("Right Shift: " + resultRightShift); // Output: -4// Unsigned Right Shiftint unsignedNumber = -16;int resultUnsignedRightShift = unsignedNumber >>> 2;System.out.println("Unsigned Right Shift: " + result7); // Output: 1}}
Conditional (Ternary) Operator:
Conditional operators, also known as ternary operators, are a shorthand way to express if-else statements on one line.Name | Operators |
Conditional operator | ? : |
public class ConditionalOperatorsExample {public static void main(String[] args) {int x = 10;String result = (number % 2 == 0) ? "Even" : "Odd";System.out.println(result); // Output: Even}}