首页 > Java > java教程 > Java 运算符

Java 运算符

王林
发布: 2024-08-30 15:18:56
原创
424 人浏览过

以下文章提供了 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中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板