The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
One of the most common operators that you'll encounter is the simple assignment operator "您将遇到的最常见的运算符之一是简单赋值运算符“=
".=
”。You saw this operator in the Bicycle class; it assigns the value on its right to the operand on its left:你在自行车课程上见过这个运算符;它将右边的值赋给左边的操作数:
int cadence = 0; int speed = 0; int gear = 1;
This operator can also be used on objects to assign object references, as discussed in Creating Objects.此运算符也可用于对象以指定对象引用,如创建对象中所述。
The Java programming language provides operators that perform addition, subtraction, multiplication, and division.Java编程语言提供了执行加法、减法、乘法和除法的运算符。There's a good chance you'll recognize them by their counterparts in basic mathematics.你很有可能会被基础数学的对手认出。The only symbol that might look new to you is "唯一对您来说可能是新的符号是“%
", which divides one operand by another and returns the remainder as its result.%
”,它将一个操作数除以另一个操作数,并返回余数作为其结果。
+ | |
- | |
* | |
/ | |
% |
The following program, 下面的程序ArithmeticDemo
, tests the arithmetic operators.ArithmeticDemo
测试算术运算符。
class ArithmeticDemo { public static void main (String[] args) { int result = 1 + 2; // result is now 3 System.out.println("1 + 2 = " + result); int original_result = result; result = result - 1; // result is now 2 System.out.println(original_result + " - 1 = " + result); original_result = result; result = result * 2; // result is now 4 System.out.println(original_result + " * 2 = " + result); original_result = result; result = result / 2; // result is now 2 System.out.println(original_result + " / 2 = " + result); original_result = result; result = result + 8; // result is now 10 System.out.println(original_result + " + 8 = " + result); original_result = result; result = result % 7; // result is now 3 System.out.println(original_result + " % 7 = " + result); } }
This program prints the following:此程序打印以下内容:
1 + 2 = 3 3 - 1 = 2 2 * 2 = 4 4 / 2 = 2 2 + 8 = 10 10 % 7 = 3
You can also combine the arithmetic operators with the simple assignment operator to create compound assignments.您还可以将算术运算符与简单赋值运算符组合以创建复合赋值。For example, 例如,x+=1;
and x=x+1;
both increment the value of x
by 1.x+=1
和x=x+1
两者都将x
的值增加1。
The +
operator can also be used for concatenating (joining) two strings together, as shown in the following ConcatDemo
program:+
运算符也可用于将两个字符串连接在一起,如以下ConcatDemo
程序所示:
class ConcatDemo { public static void main(String[] args){ String firstString = "This is"; String secondString = " a concatenated string."; String thirdString = firstString+secondString; System.out.println(thirdString); } }
By the end of this program, the variable 在本程序结束时,变量thirdString
contains "This is a concatenated string.", which gets printed to standard output.thirdString
包含“This is a concatenated string.”,它被打印到标准输出。
The unary operators require only one operand; they perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean.一元运算符只需要一个操作数;它们执行各种操作,例如将值递增/递减1、对表达式求反或反转布尔值。
+ | |
- | |
++ | |
-- | |
! |
The following program, 下面的程序UnaryDemo
, tests the unary operators:UnaryDemo
测试一元运算符:
class UnaryDemo { public static void main(String[] args) { int result = +1; // result is now 1 System.out.println(result); result--; // result is now 0 System.out.println(result); result++; // result is now 1 System.out.println(result); result = -result; // result is now -1 System.out.println(result); boolean success = false; // false System.out.println(success); // true System.out.println(!success); } }
The increment/decrement operators can be applied before (prefix) or after (postfix) the operand.递增/递减运算符可以应用于操作数之前(前缀)或之后(后缀)。The code 代码result++;
and ++result;
will both end in result
being incremented by one.result++;
和++result;
,result
都将增加1。The only difference is that the prefix version (唯一的区别是前缀版本(++result
) evaluates to the incremented value, whereas the postfix version (result++
) evaluates to the original value.++result
)的计算结果为递增值,而后缀版本(result++
)的计算结果为原始值。If you are just performing a simple increment/decrement, it doesn't really matter which version you choose.如果您只是执行一个简单的递增/递减操作,那么选择哪个版本并不重要。But if you use this operator in part of a larger expression, the one that you choose may make a significant difference.但是,如果在较大表达式的一部分中使用此运算符,则选择的运算符可能会产生显著的差异。
The following program, 以下程序PrePostDemo
, illustrates the prefix/postfix unary increment operator:PrePostDemo
演示了前缀/后缀一元增量运算符:
class PrePostDemo { public static void main(String[] args){ int i = 3; i++; // prints 4 System.out.println(i); ++i; // prints 5 System.out.println(i); // prints 6 System.out.println(++i); // prints 6 System.out.println(i++); // prints 7 System.out.println(i); } }