
Operators are special symbols in Java that perform specific operations on one, two, or three operands and return a result. Operators are used to manipulate variables and values in a program. They can be used to perform mathematical calculations, make comparisons, and control the flow of a program. This tutorial will take a closer look at the different types of operators available in Java and how they are used in a program. We will also cover some best practices for using operators in Java and how to work with operator precedence and associativity. By the end of this tutorial, you will have a solid understanding of how to use operators in Java to write efficient and effective code.
- Types of Operators in Java
- Java Arithmetic Operators
- Java Relational Operators
- Java Logical Operators
- Java Bitwise Operators
- Java Assignment Operators
- Java Ternary Operator
- Operator Precedence and Associativity in Java
Types of Operators in Java
Java provides several types of operators for different operations. These include:
- Arithmetic Operators: These operators are used for mathematical calculations such as addition, subtraction, multiplication, and division.
- Relational Operators: These operators are used to compare values and determine their relationship. They include operators such as greater than ( > ), less than ( < ), equal to ( == ), and not equal to ( != ).
- Logical Operators: These operators are used to evaluate logical expressions and make decisions in a program. They include operators such as AND ( && ), OR ( || ), and NOT ( ! ).
- Bitwise Operators: These operators are used to manipulate individual bits of a value. They include operators such as AND ( & ), OR ( | ), and NOT ( ~ ).
- Assignment Operators: These operators are used to assign a value to a variable. The most common assignment operator is the equal sign ( = ).
- Ternary Operator: This operator is a shorthand way of writing an if-else statement. It takes three operands and assigns the second operand to a variable if the first operand is true, otherwise it assigns the third operand.
- Other Operators: There are also other types of operators like instanceof, which is used to check the type of an object, and the conditional operator ( ?: ), also known as the ternary operator, which is used to assign a value to a variable based on a condition.
Java Arithmetic Operators
Java provides several arithmetic operators that can be used to perform mathematical calculations on operands. These include:
- Addition (+): This operator adds two operands together and returns the sum.
- Subtraction (-): This operator subtracts the second operand from the first and returns the difference.
- Multiplication (*): This operator multiplies two operands together and returns the product.
- Division (/): This operator divides the first operand by the second and returns the quotient.
- Modulus (%): This operator returns the remainder of dividing the first operand by the second.
- Increment (++): This operator increments the value of the operand by 1.
- Decrement (–): This operator decrements the value of the operand by 1.
These operators follow the standard mathematical order of operations (PEMDAS) when used in an expression. For example, in the expression “5 + 2 * 3”, the multiplication operator (*) is performed before the addition operator (+) and the result is 11. Also, these operators can be used on all numeric primitive types such as int, long, short, byte, and double.
Java Relational Operators
Java provides several relational operators that can be used to compare values and determine their relationship. These operators return a boolean value of true or false. These include:
- Greater Than ( > ): This operator returns true if the first operand is greater than the second operand.
- Less Than ( < ): This operator returns true if the first operand is less than the second operand.
- Greater Than or Equal To ( >= ): This operator returns true if the first operand is greater than or equal to the second operand.
- Less Than or Equal To ( <= ): This operator returns true if the first operand is less than or equal to the second operand.
- Equal To ( == ): This operator returns true if the first operand is equal to the second operand.
- Not Equal To ( != ): This operator returns true if the first operand is not equal to the second operand.
These operators can be used to compare numeric, boolean and object data types, but it’s important to note that when comparing object references, these operators compares the memory addresses of the objects, not their content.
When you want to compare the content of the object, you can use the equals() method of the object that you want to compare.
Java Logical Operators
Java provides several logical operators that can be used to evaluate logical expressions and make decisions in a program. These operators return a boolean value of true or false. These include:
- Logical AND ( && ): This operator returns true if both the first and second operands are true.
- Logical OR ( || ): This operator returns true if either the first or second operand is true.
- Logical NOT ( ! ): This operator returns the opposite of the boolean value of the operand. If the operand is true, the operator returns false. If the operand is false, the operator returns true.
The && and || operators use short-circuit evaluation, which means that if the first operand can determine the final result, the second operand will not be evaluated. This can be useful for avoiding unnecessary computation or for handling null values.
These operators can be used with boolean values or expressions that evaluate to boolean values. They are commonly used in conditional statements such as if and while to control the flow of a program.
Java Bitwise Operators
Java provides several bitwise operators that can be used to manipulate individual bits of an integer value. These operators perform their operations on the binary representation of the operand and return a new integer value. These include:
- Bitwise AND ( & ): This operator compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
- Bitwise OR ( | ): This operator compares each bit of the first operand to the corresponding bit of the second operand. If either bit is 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
- Bitwise XOR ( ^ ): This operator compares each bit of the first operand to the corresponding bit of the second operand. If the bits are different, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
- Bitwise NOT ( ~ ): This operator inverts all the bits of the operand.
- Left Shift ( << ): This operator shifts the bits of the operand to the left by the specified number of places.
- Right Shift ( >> ): This operator shifts the bits of the operand to the right by the specified number of places.
- Unsigned Right Shift ( >>> ): This operator also shifts the bits of the operand to the right by the specified number of places, but it always fills the leftmost bits with 0.
These operators are mainly used to manipulate integers and are not applicable for other data types. They are commonly used in low-level programming, such as working with memory or network protocols, or in optimization techniques.
Java Assignment Operators
Java provides several assignment operators that can be used to assign a value to a variable. These include:
- Simple Assignment ( = ): This operator assigns the value of the second operand to the first operand.
- Add and Assign ( += ): This operator adds the value of the second operand to the first operand and assigns the result to the first operand.
- Subtract and Assign ( -= ): This operator subtracts the value of the second operand from the first operand and assigns the result to the first operand.
- Multiply and Assign ( *= ): This operator multiplies the value of the first operand by the second operand and assigns the result to the first operand.
- Divide and Assign ( /= ): This operator divides the value of the first operand by the second operand and assigns the result to the first operand.
- Modulus and Assign ( %= ): This operator calculates the remainder of dividing the first operand by the second operand and assigns the result to the first operand.
- Left Shift and Assign ( <<= ): This operator shifts the bits of the first operand to the left by the specified number of places and assigns the result to the first operand.
- Right Shift and Assign ( >>= ): This operator shifts the bits of the first operand to the right by the specified number of places and assigns the result to the first operand.
- Bitwise AND and Assign ( &= ): This operator performs a bitwise AND operation on the first and second operands and assigns the result to the first operand.
- Bitwise OR and Assign ( |= ): This operator performs a bitwise OR operation on the first and second operands and assigns the result to the first operand.
- Bitwise XOR and Assign ( ^= ): This operator performs a bitwise XOR operation on the first and second operands and assigns the result to the first operand.
These operators are used to assign a value to a variable, perform an operation and then assign the result to the variable in one step. They are commonly used to simplify the code and make it more readable.
Java Ternary Operator
The Ternary operator in Java is a shorthand way of writing an if-else statement. It takes three operands and assigns the second operand to a variable if the first operand is true, otherwise it assigns the third operand. The general syntax is:
variable = (condition) ? value_if_true : value_if_false;
For example:
int x = 5, y = 10;
int max = (x > y) ? x : y;
In this example, the ternary operator compares x and y, and assigns the greater value to the variable max. So, if x is greater than y, max will be assigned the value of x, otherwise max will be assigned the value of y.
The ternary operator is commonly used in situations where a simple if-else statement would be verbose or repetitive. It can be used to assign a value to a variable based on a condition or to return a value from a method based on a condition.
The ternary operator can only return one value, if you need to return multiple values based on a condition, you can use if-else statement or other control structures like switch statement.
Operator Precedence and Associativity in Java
Operator precedence refers to the order in which operators are evaluated in an expression. Operators with higher precedence are evaluated before operators with lower precedence. For example, in the expression “5 + 2 * 3”, the multiplication operator (*) has higher precedence than the addition operator (+), so the multiplication is done first, resulting in the value 11.
Java follows the standard mathematical order of operations, also known as PEMDAS (Parentheses, Exponents, Multiplication and Division, Addition and Subtraction), when evaluating expressions. Operators with the same precedence are evaluated from left to right.
Operator associativity, on the other hand, refers to the direction in which an operator is applied when multiple operators of the same precedence are used in an expression. In Java, most operators are left-associative, meaning that they are applied from left to right. For example, in the expression “5 – 2 – 1”, the subtraction operator (-) is left-associative, so the expression is evaluated as (5 – 2) – 1 = 2.
It’s important to know operator precedence and associativity when working with complex expressions to ensure that the intended operation is performed. Parentheses can be used to force a certain order of operations, if necessary.