Java Operators: Boost Your Coding Skills

Piyu Jain
JavaToDev
Published in
5 min readApr 13, 2023

Introduction to Operators

Operators in Java are symbols used to perform various operations on operands, such as arithmetic, comparison, and logical operations. They are an essential part of the Java programming language.

  • Definition of Operators: In Java programming language, operators are symbols that represent operations or actions to be performed on operands or values. Operators are used for arithmetic, comparison, logical, and other types of operations.

Types of Operators

— Arithmetic Operators

  1. Addition (+): Adds two operands
  2. Subtraction (-): Subtracts one operand from another
  3. Multiplication (*): Multiplies two operands
  4. Division (/): Divides one operand by another
  5. Modulus (%): Returns the remainder of dividing one operand by another
  6. Increment (++): Increases the value of an operand by 1
  7. Decrement ( — ): Decreases the value of an operand by 1.

These operators can be used with numeric data types such as int, float, double, long, and short in Java.

— Relational Operators

Relational operators are used to compare two values and return a boolean value (true or false) based on the comparison. In Java, the following relational operators are available:

  1. Greater than (>): This operator returns true if the value on the left is greater than the value on the right.
  2. Less than (<): This operator returns true if the value on the left is less than the value on the right.
  3. Greater than or equal to (>=): This operator returns true if the value on the left is greater than or equal to the value on the right.
  4. Less than or equal to (<=): This operator returns true if the value on the left is less than or equal to the value on the right.
  5. Equal to (==): This operator returns true if the value on the left is equal to the value on the right.
  6. Not equal to (!=): This operator returns true if the value on the left is not equal to the value on the right.

— Logical Operators

logical operators are used to performing logical operations on boolean values. There are three logical operators:

  1. && (logical AND): returns true if and only if both operands are true. Otherwise, it returns false.
  2. || (logical OR): returns true if either one of the operands is true. If both operands are false, it returns false.
  3. ! (logical NOT): returns true if the operand is false, and false if the operand is true.

Logical operators are commonly used in decision-making and conditional statements to check multiple conditions at once.

— Bitwise Operators

Bitwise operators are used to perform bitwise operations on integer types. Bitwise operators perform operations on each individual bit of the operands.

The following are the bitwise operators in Java:

  1. AND (&)
  2. OR (|)
  3. XOR (^)
  4. NOT (~)
  5. Signed Left Shift (<<)
  6. Signed Right Shift (>>)
  7. Unsigned Right Shift (>>>)

The AND, OR, and XOR operators compare each bit of the operands and return a new integer that contains the result. The NOT operator returns the bitwise complement of the operand, which means it reverses all of the bits. The shift operators move the bits to the left or right by a certain number of positions.

Bitwise operators are often used in low-level programming, such as device drivers or cryptography, to manipulate individual bits of data.

— Assignment Operators

an assignment operator is used to assign a value to a variable. The basic assignment operator is the equal sign (=), which assigns the value on its right to the variable on its left. For example, “int x = 5;” assigns the value 5 to the variable x.

Java also provides compound assignment operators, which combine an arithmetic operator with the basic assignment operator. These operators allow you to perform an arithmetic operation and assign the result to the same variable in one step. For example, “x += 5;” is equivalent to “x = x + 5;”.

Here are the compound assignment operators in Java:

  1. += : Addition and assignment
  2. -= : Subtraction and assignment
  3. *= : Multiplication and assignment
  4. /= : Division and assignment
  5. %= : Modulus and assignment
  6. &= : Bitwise AND and assignment
  7. |= : Bitwise OR and assignment
  8. ^= : Bitwise XOR and assignment
  9. <<= : Left shift and assignment
  10. >>= : Right shift and assignment
  11. >>>= : Unsigned right shift and assignment

— Ternary Operator

The Ternary Operator in Java is also known as the conditional operator. It is a shorthand version of an if-else statement and is used to simplify code. The ternary operator consists of three operands: a condition, a value to return if the condition is true, and a value to return if the condition is false. It is represented by the “?” and “:” symbols, where the condition appears before the “?” symbol, the value to return if true appears after the “?” symbol, and the value to return if false appears after the “:” symbol.

The general syntax for the ternary operator in Java is:

(condition) ? (value if true) : (value if false);

Here’s an example that uses the ternary operator to determine if a number is even or odd:

int number = 10;
String result = (number % 2 == 0) ? "even" : "odd";
System.out.println(result);

— Operator Precedence

Operator precedence in Java determines the order of execution of operators in an expression. It is important to understand the precedence rules because it affects the final result of the expression.

The following is the operator precedence hierarchy in Java, listed in descending order of precedence:

  1. Postfix operators: expr++, expr —
  2. Unary operators: ++expr, — expr, +expr, -expr, ~, !
  3. Multiplicative operators: *, /, %
  4. Additive operators: +, -
  5. Shift operators: <<, >>, >>>
  6. Relational operators: <, <=, >, >=, instanceof
  7. Equality operators: ==, !=
  8. Bitwise AND operator: &
  9. Bitwise XOR operator: ^
  10. Bitwise OR operator: |
  11. Logical AND operator: &&
  12. Logical OR operator: ||
  13. Ternary operator: ? :
  14. Assignment operators: =, +=, -=, *=, /=, %=, <<=, >>=, >>>=, &=, ^=, |=

Operators with higher precedence are evaluated first. If operators have the same precedence, they are evaluated from left to right.

For example, in the expression 2 + 3 * 4, the multiplication operator (*) has higher precedence than the addition operator (+). Therefore, the expression will be evaluated as 2 + (3 * 4), which results in 14.

It is important to use parentheses to override the default precedence of operators if needed. For example, (2 + 3) * 4 will evaluate to 20, because the addition operator is evaluated first due to the use of parentheses.

--

--

Piyu Jain
JavaToDev

Write about #Java #Technology, #AI, #Book Summary, #Motivation, #Earning