Documentation

The Java™ Tutorials
Hide TOC
Operators运算符
Trail: Learning the Java Language
Lesson: Language Basics

Operators

Now that you've learned how to declare and initialize variables, you probably want to know how to do something with them.既然您已经学习了如何声明和初始化变量,您可能想知道如何使用它们。Learning the operators of the Java programming language is a good place to start.学习Java编程语言的操作符是一个很好的起点。Operators are special symbols that perform specific operations on one, two, or three operands, and then return a result.运算符是对一个、两个或三个操作数执行特定操作,然后返回结果的特殊符号。

As we explore the operators of the Java programming language, it may be helpful for you to know ahead of time which operators have the highest precedence.在我们探索Java编程语言的运算符时,提前知道哪些运算符具有最高优先级可能会有所帮助。The operators in the following table are listed according to precedence order.下表中的运算符按优先顺序列出。The closer to the top of the table an operator appears, the higher its precedence.运算符越靠近表的顶部,其优先级越高。Operators with higher precedence are evaluated before operators with relatively lower precedence.优先级较高的运算符在优先级相对较低的运算符之前求值。Operators on the same line have equal precedence.同一行上的运算符具有相同的优先级。When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first.当同等优先级的运算符出现在同一表达式中时,必须有一条规则来控制首先求值的运算符。All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.除赋值运算符外,所有二进制运算符都从左到右求值;赋值运算符从右向左求值。

Operator Precedence运算符优先级
Operators运算符 Precedence优先级
postfix后缀 expr++ expr--
unary一元 ++expr --expr +expr -expr ~ !
multiplicative乘法 * / %
additive添加剂 + -
shift移动 << >> >>>
relational关系 < > <= >= instanceof
equality平等 == !=
bitwise AND按位与 &
bitwise exclusive OR按位异或 ^
bitwise inclusive OR按位包含或 |
logical AND逻辑与 &&
logical OR逻辑或 ||
ternary三元 ? :
assignment赋值 = += -= *= /= %= &= ^= |= <<= >>= >>>=

In general-purpose programming, certain operators tend to appear more frequently than others; for example, the assignment operator "=" is far more common than the unsigned right shift operator ">>>".在通用编程中,某些操作符往往比其他操作符出现得更频繁;例如,赋值运算符“=”远比无符号右移运算符“>>>”常见。With that in mind, the following discussion focuses first on the operators that you're most likely to use on a regular basis, and ends focusing on those that are less common.考虑到这一点,下面的讨论首先集中在您最有可能定期使用的操作符上,最后集中在那些不太常见的操作符上。Each discussion is accompanied by sample code that you can compile and run.每一次讨论都伴随着您可以编译和运行的示例代码。Studying its output will help reinforce what you've just learned.研究它的输出将有助于巩固你刚刚学到的东西。


Previous page: Questions and Exercises: Variables
Next page: Assignment, Arithmetic, and Unary Operators