首頁  >  文章  >  Java  >  Java 運算符

Java 運算符

王林
王林原創
2024-08-30 15:18:56354瀏覽

以下文章提供了 Java 運算符的概述。 Java 運算子表示一種有助於對一個或多個運算元執行多種運算的符號。運算符可以是 +、-、/、* 等,取決於需求。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

有不同類型的運算符。

  • 算術運算子
  • 位元運算子
  • 賦值運算子
  • 三元運算子
  • 自增和自減運算子
  • 比較運算子或關係運算子
  • 邏輯運算子
  • 輪班運算子

Java 中的運算子類型

下面給出了java中運算子的類型:

1.算術運算子

算術運算子用於執行多種算術運算。

以下是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.位元運算子

位元運算子常用於在 Java 中執行移位和位元運算。

以下是常用的位元運算子。

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.賦值運算子

賦值運算子用於為某些變數賦值。

以下是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.三元運算子

在Java中,三元運算子主要用於if-then-else條件替換。它是一種在 Java 程式設計中廣泛使用的單行語句,只需要 3 個運算元。

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.自動遞增運算子和自動遞減運算子

Java 中的自動遞增和自動遞減運算子有助於分別將值遞增和遞減 1。

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.比較運算子或關係運算子

關係運算子是有助於檢查運算元是否相等的運算子。除此之外,這些運算子也用於比較 2 個或更多值。

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.邏輯運算子

Java 中的邏輯運算子常用於布林運算式。

下面是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.移位運算子

Java 中的移位運算子用於根據需要向左或向右移動位元。

以下是 Shift 運算子。

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

讓我們看看這些運算子的優先順序如何。

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 = += -= *= /= %= &= ^= |= <<= >>= >>>=

以上是Java 運算符的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:Java 標識符下一篇:Java 標識符