🅾️Operators
Last updated
Last updated
While learning Java, you will come across the concept of operators. What is a Java operator? Java operators are symbols useful for performing operations on variables. They also help manipulate the values of the operands.
The different Java operators are useful for performing various functions. Dive into this article to learn more about Java operators, their types, and their importance.
Once you know what is a Java operator, you must consider its importance. You should know that operators in Java are extremely vital. Without Java operators, you won’t be able to make logical, arithmetic calculations in different programs. According to the type of Java operators, they manipulate a logical or arithmetic value or operand in a certain way to deliver a specific outcome. Operators in Java play a crucial role in the programming world by dealing with simple arithmetic functions and executing complex algorithms. Java operators are also responsible for key functions like security encryption.
Now that you know what is a Java operator, you should learn about the different types of operators in Java.
Arithmetic Operators
Ternary Operators
Bitwise Operator
Relational Operators
Logical Operators
Unary Operators
Assignment Operator
Shift Operator in Java
Arithmetic operators in Java are useful for executing addition, multiplication, division, subtraction, and modulus. As you can see, these operators in Java are valuable for mathematical operations.
Examples of Arithmetic Operators
Addition (+)
The addition operator (+) is used to add two operands. The operands can be numeric, string or a combination of both.
When adding numbers, it will perform numerical addition. When adding strings, it will concatenate them. Adding a number and string will convert the number to a string and concatenate.
Subtraction (-)
The subtraction operator (-) subtracts the right operand from the left operand. Both operands must be numeric.
Multiplication (*)
The multiplication operator (*) multiplies two numeric operands.
Division (/)
The division operator (/) divides the left operand by the right one. It performs floating point or integer division depending on the operands.
Modulus (%)
The modulus operator (%) returns the remainder left over after dividing the left operand by the right. Both operands must be integers.
What is the ternary operator in Java?
The ternary operator in Java is a shorthand way of writing an if-else
statement. It allows you to evaluate a condition and execute one block of code if the condition is true, and another block of code if the condition is false.
Syntax
The syntax consists of three parts:
The condition to evaluate which must return a boolean value
The expression to execute if the condition is true followed by a question mark ?
The expression to execute if the condition is false followed by a colon :
Example
Here we evaluate if the variable num
is greater than 5. If true, we set result
to "Greater than 5", otherwise we set it to "Less than or equal to 5".
Advantages
More concise syntax compared to if-else statements
Improves readability when used for simple conditional checks
The ternary operator provides a shorthand way to write simple conditional logic in a concise, easy to read manner.
The bitwise operator in Java operates on a bit array, bit string, or binary number. The fast and simple operator in Java is directly supported by the processor. You can also refer to the bitwise operation as bit-level programming.
Examples of Bitwise Operators in Java
What are relational operators?
Relational operators are used to compare two values and determine the relationship between them. They evaluate to a boolean result of either true or false.
Types of relational operators
==
- Equal to
!=
- Not equal to
>
- Greater than
<
- Less than
>=
- Greater than or equal to
<=
- Less than or equal to
Equal to (==)
Compares if two values are equal. The result is true if they are equal, false otherwise.
Not equal to (!=)
Compares if two values are not equal. The result is true if they are not equal, false otherwise.
Greater than (>)
Compares if the left operand is greater than the right operand. The result is true if left is greater, false otherwise.
The other relational operators work similarly to compare the relationship between two values.
Logical operators in Java help combine two or more conditions. It is useful for performing the original condition under consideration.
Examples of Logical Operators in Java
The unary operators in Java demand a single operand. These Java operators can increase or decrease the value. As a result, an expression becomes negative, or the boolean value gets inverted.
Examples of Unary Operators in Java
Used to increase (or decrease) the value of a variable by 1
Easy to use, important to recognize
Uses current value of variable, THEN increments it
Increments variable first, THEN uses new value
result = 15
m=5
result = 12
m=5
The assignment operators in Java are beneficial for allocating new values to a variable. In assignment operators in Java, the left side is called a variable. The right side of these Java operators is called value.
Examples of Assignment Operators in Java
Operator | Description | Example |
---|---|---|
+ (Addition)
Adds values on either side of the operator.
A + B will give 30
- (Subtraction)
Subtracts right-hand operand from left-hand operand.
A - B will give -10
* (Multiplication)
Multiplies values on either side of the operator.
A * B will give 200
/ (Division)
Divides left-hand operand by right-hand operand.
B / A will give 2
% (Modulus)
Divides left-hand operand by right-hand operand and returns remainder.
B % A will give 0
++ (Increment)
Increases the value of operand by 1.
B++ gives 21
-- (Decrement)
Decreases the value of operand by 1.
B-- gives 19