Operators in Java make up a crucial part of this programming language s foundation. You wouldn t get far without being able to perform simple operations. And that s why operators are a fundamental and important part of Java, allowing for the most common operations in programming.
Performing calculations, determining true or false, figuring out relations you need operators to do anything. And that doesn t go for just Java, but every other language out there.
In this blog post, we will go over the most used operators in Java, as well as some examples so you can see how they work. Let s get to it!
What are Operators in Java?
Operators are frequently used in programming. At a basic level, operators are symbols that allow you to operate on one or more values, known as operands. Whether it s the assignment operator (=) or the ternary operator (?), programs are often littered with them. We will now go over the most common types of operators.
Arithmetic Operators
As the name suggests, arithmetic operators allow you to perform mathematical operations. They will work on any numeric data type.
Addition (+)
The addition operator allows you to add two numbers together. Here,sumwill be the sum of 3 and 5:
int sum = 3 + 5; // sum will be 8 |
It can also be used to concatenate (or add) two strings together. Here,resultbecomes the concatenation of Hello and World!
String result = Hello + World! ; // result will be Hello World! |
Subtraction (-)
The subtraction operator allows you to subtract one number from another. Here,differencewill be the difference of 5 and 3:
int difference = 5 3; // difference will be 2 |
Multiplication (*)
The multiplication operator allows you to multiply two numbers. Here,productwill be the product of 5 and 3:
int product = 5 * 3; // product will be 15 |
Division (/)
The division operator allows you to divide two numbers. If both operands are integers, then it will beflooreddivision, meaning the quotient is floored. To avoid this, at least one operand must be a floating-point number. Here,quotientwill be the floored quotient of 5 and 3:
int quotient = 5 / 3; // quotient will be 1 |
Modulo (%)
The modulo operator allows you to take one numbermoduloanother number. In other words, it gives you the remainder after dividing one integer by another. Here,remainderwill be the remainder of 10 modulo 3:
int remainder = 10 % 3; // remainder will be 1 |
Increment (++)
The increment operator increments a variable by 1. If the operator is before the variable, the variable will be incremented by 1, and the value after incrementing will be returned. However, if it is after the variable, the value before incrementing will be returned.
int a = 5; a++; // a will be 6 |
Decrement ( )
The decrement operator decrements a variable by 1. If the operator is before the variable, the variable will be decremented by 1, and the value after decrementing will be returned. However, if it is after the variable, the value before decrementing will be returned.
int a = 5; a ; // a will be 4 |
Assignment Operators
Assignment Operator (=)
The assignment operator is quite simple: it allows you to assign a value to a variable. For example, if you write:
int a = 5; |
Thenawill be 5.
Compound Assignment Operators
The assignment operator can be combined with other arithmetic and bitwise operators (these will be covered later.) This means that instead of having to write:
[variable] = [variable] [operator] [operand] |
You can write:
[variable] [compound assignment operator] [operand] |
Confused? These examples should help. This includes all compound assignment operators:
int x = 10; x += 5; // Equivalent to x = x + 5; (x will be 15) int y = 8; int z = 6; int a = 16; int b = 11; int c = 9; int d = 7; int e = 10; int f = 16; int g = 32; int h = -16; |
Comparison Operators
Comparison operators compare two operators and return aboolean(true or false) depending on the result.
Equal To (==)
The equal to operator checks if two values are equal to each other.
int a = 5; int b = 3; boolean result = (a == b); // result will be false |
Not Equal To (!=)
The not equal to operator checks if two values are not equal to each other.
int a = 5; int b = 3; boolean result = (a != b); // result will be true |
Greater Than (>)
The greater than operator checks if the left operand is greater than the right operand.
int a = 5; int b = 3; boolean result = (a > b); // result will be true |
Less Than (<)
The less than operator checks if the left operand is less than the right operand.
int a = 5; int b = 3; boolean result = (a < b); // result will be false |
Greater Than or Equal To (>=)
The greater than or equal to operator checks if the two operands are equal or if the left operand is greater than the right operand.
int a = 5; int b = 3; boolean result = (a < b); // result will be false |
Less Than or Equal To (<=)
The less than or equal to operator checks if the two operands are equal or if the left operand is less than the right operand.
int a = 5; int b = 3; boolean result = (a <= b); // result will be false |
Logical Operators
Logical operatorsoperate on booleans and return a boolean depending on the relationship between the operands. Note that the operands do not have to be literal booleans (trueorfalse). They can also be and usually are their own expressions that evaluate to a boolean.
Logical AND (&&)
The logical AND operator checks if the two operands are both true.
boolean a = true; boolean b = false; boolean result = (a && b); // result will be false |
Logical OR (||)
The logical OR operator checks if either or both of the operands are true.
boolean a = true; boolean b = false; boolean result = (a || b); // result will be true |
Logical NOT (!)
The logical NOT operator reverses the truth value of the operand.
boolean a = true; boolean result = !a; // result will be false |
Bitwise Operators
Abitwise operatoris one that operates on values at abit level.They will return different numbers depending on the operands.
Bitwise AND (&)
The bitwise AND operator will return a number that has 1 s where both operands have 1 s in their bits and 0 s otherwise.
int a = 5; // Binary: 00000101 int b = 3; // Binary: 00000011 int result = a & b; // Binary: 00000001 (result will be 1) |
Bitwise OR (|)
The bitwise OR operator will return a number that has 1 s where either or both operands have 1 s in their bits and 0 s otherwise.
int a = 5; // Binary: 00000101 int b = 3; // Binary: 00000011 int result = a | b; // Binary: 00000111 (result will be 7) |
Bitwise XOR (^)
The bitwise XOR operator will return a number that has 1 s where either but not both of the operands have 1 s in their bits and 0 s otherwise.
int a = 5; // Binary: 00000101 int b = 3; // Binary: 00000011 int result = a ^ b; // Binary: 00000110 (result will be 6) |
Bitwise NOT (~)
The bitwise NOT operator will return a number with 1 s where the operand has 0 s in its bits and 0 s where the operand has 1 s.
int a = 5; // Binary: 00000101 int result = ~a; // Binary: 11111010 (result will be -6) |
Left Shift (<<)
The left shift operator will return a number that has all the bits of the left operand shifted to the left by the number of times specified by the right operand.
int a = 5; // Binary: 00000101 int result = a << 2; // Binary: 00010100 (result will be 20) |
Right Shift (>>)
The right shift operator will return a number that has all the bits of the left operand shifted to the right by the number of times specified by the right operand. The new bit positions on the left will be filled in with the original sign bit (the first bit).
int a = 16; // Binary: 00010000 int result = a >> 2; // Binary: 00000100 (result will be 4) |
Unsigned Right Shift (>>>)
The unsigned right shift operator will return a number that has all the bits of the left operand shifted to the right by the number of times specified by the right operand, but all bits on the left will be filled in with 0 s, regardless of the sign of the number.
int a = -16; // Binary: 11110000 int result = a >>> 2; // Binary: 00111100 (result will be 60) |
Ternary Operator (?)
The ternary operator got its name from the number of operands it works on: three.
The logical NOT operator is a unary operator, because it works on one operand. The addition operator is abinary operatorbecause it works on two operands. Thus, the operator that works on three operands is ternary. Makes sense, right?
The ternary operator is a concise way of writing conditional expressions in Java. It takes the general form:
[condition] ? [true_expression] : [false_expression] |
Essentially, if the condition is true, thenthe ternary operatorwill return the true expression, and if the condition is false, it will return the false expression. For example, here s how you could determine the max number using it:
int a = 5; int b = 3; int max = (a > b) ? a : b; // max will be equal to a |
Operators in Java: Wrapping Up
Operators are an essential part of Java, and they are going to be used in any type of task you accomplish through programming. By knowing all of them, you will be best equipped to deal with problems, as you may know, thesimplestway.
There is no way to avoid operators in Java if you are trying to master this language. So, we encourage you to learn more about operators and make the most of them while programming!