Home  >  Article  >  Java  >  Java Operators

Java Operators

王林
王林Original
2024-08-30 15:18:56380browse

The following article provides an outline for Java Operators. Java operator denotes a symbol that helps in performing several operations on one or more than one operand. Operators can be +, -, /, *, etc., depending on the requirement.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

There are different types of operators.

  • Arithmetic Operators
  • Bitwise Operators
  • Assignment Operators
  • Ternary Operator
  • Auto-increment & Auto-decrement Operators
  • Comparison operators or Relational Operators
  • Logical Operators
  • Shift Operators

Types of Operators in Java

Given below are the types of operators in java:

1. Arithmetic Operators

Arithmetic Operators are used to perform several arithmetic operations.

The following are the arithmetic operators in Java.

Operator Name/ Symbol Definition Example
Addition(+) It helps in adding two values. A+B
Subtraction(-) It helps in subtracting two values. A-B
Modulus(%) It helps in getting the remainder obtained by dividing two values. A%B
Multiplication( * ) It helps in multiplying two values. A*B
Division ( / ) Helps in dividing two values. A/B

2. Bitwise Operators

Bitwise operators are commonly used in performing bit shift and bitwise operations in Java.

Below are the Bitwise operators that are commonly used.

Operator Name/ Symbol Definition Example
Bitwise AND operator (&) It helps in comparing 2 operand’s corresponding bits. 1 will be returned if both the bits are 1; else 0 will be returned. A&B
Bitwise OR operator (|) It helps in comparing 2 operand’s corresponding bits. 1 will be returned if one of the bit is 1; else 0 will be returned. A|B
Bitwise XOR operator (^) It helps in comparing 2 operand’s corresponding bits. 1 will be returned if the corresponding bits are dissimilar, else 0 will be returned. A^B
Bitwise complement operator (~) It helps in inverting the pattern of bits. That is, 1 will be changed to 0 and 0 will be changed to 1. ~B

3. Assignment Operators

Assignment Operators are used for assigning values to certain variables.

The following are the assignment operators in Java.

Operator Name/ Symbol Definition Example
-= It helps subtract the right and left operators, thereby assigning the obtained result to the operator on the left. A-=B
/= Helps in dividing the right and left operators and thereby assigning the obtained result to the operator in the left. A/=B
*= It helps multiply right and left operators and thereby assign the obtained result to the operator on the left. A*=B
+= It helps add right and left operators and thereby assign the obtained result to the operator on the left. A+=B
^= Left operand value will be raised to the right operator power. A^=B
%= A modulus operator will be applied. A%=B

4. Ternary Operators

In Java, the Ternary operator is mainly used for if-then-else condition replacement. It is a one-line statement that is widely used in Java programming, which takes only 3 operands.

Operator Name/ Symbol Definition Example
condition?v1: v2 V1 will be returned if the condition mentioned is true, and v2 will be returned if the condition is false. A>B?A:B

5. Auto Increment Operators and Auto Decrement Operators

Auto Increment and Auto Decrement operators in Java helps in incrementing and decrementing the values by 1, respectively.

Operator Name/ Symbol Definition Example
++ The value will be incremented by 1. A++; It is similar to A+=1
The value will be decremented by 1. A–; It is similar to A-=1

6. Comparison Operators or Relational Operators

Relational operators are operators that helps in checking the operand’s equality. In addition to that, these operators are also used for comparing 2 or more values.

Operator Name/ Symbol Definition Example
equals to(==) If the left operand is equal to the right operand, true will be returned; else, false. A==B
not equal to(!=) If the left operand is not equal to the right operand, true will be returned; else, false. A!=B
less than(<)  If the left operand is less than the right operand, true will be returned; else, false. A
greater than(>) If the left operand is greater than the right operand, true will be returned; else, false. A>B
greater than or equal to(>=) If the left operand is greater than or equal to the right operand, true will be returned else, false. A>=B
Less than or equal to(<=) If the left operand is less than or equal to the right operand, true will be returned else, false. A<=B

7. Logical Operators

Logical Operators in Java are commonly used in Boolean expressions.

Below are the logical operators in Java.

Operator Name/ Symbol Definition Example
Conditional AND(&&) If both the Boolean expressions are satisfied, true will be returned else, false. AD
Conditional OR(||) If any of the Boolean expression is satisfied, true will be returned; else, false. AD
Logical NOT(!) It helps in inverting the operand’s logical state. That is, 1 will be changed to 0 and 0 will be changed to 1. !B

8. Shift Operators

Shift operators in Java are used to shift the bits left or right based on the requirement.

The following are the Shift operators.

Operator Name/ Symbol Definition Example
Left Shift Operator ( << ) X in binary notation will be shifted y bits left by shifting the zeroes from the right. A<<2
Right Shift Operator ( >> ) X in binary notation will be shifted y bits right by discarding shifted bits. B>>2
Unsigned Right Shift Operator ( >>> ) X in binary notation will be shifted y bits right by discarding shifted bits and shifting the zeroes from the right. A>>>2

Let us see how the precedence of these operators will be.

Operator Precedence
Postfix Operators Exp++ Exp–
Unary Operators ++Exp –Exp +_Exp –Exp~ !
Multiplicative Operators * / %
Additive Operators + –
Shift Operators << >> >>>
Relational Operators < > <= >= instanceof
Equality Operators == !=
Bitwise AND Operator &
Bitwise exclusive OR Operator ^
Bitwise inclusive OR Operator |
Logical AND Operator &&
Logical OR Operator ||
Ternary Operators ?:
Assignment Operators = += -= *= /= %= &= ^= |= <<= >>= >>>=

The above is the detailed content of Java Operators. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Java IdentifiersNext article:Java Identifiers