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发行说明。
Now that you understand variables and operators, it's time to learn about expressions, statements, and blocks.既然您已经了解了变量和运算符,那么现在是学习表达式、语句和块的时候了。Operators may be used in building expressions, which compute values; expressions are the core components of statements; statements may be grouped into blocks.运算符可用于计算值的建筑表达式中;表达式是语句的核心组成部分;语句可以分组到块中。
An expression is a construct made up of variables, operators, and method invocations, which are constructed according to the syntax of the language, that evaluates to a single value.表达式是由变量、运算符和方法调用组成的构造,这些变量、运算符和方法调用是根据计算为单个值的语言语法构造的。You've already seen examples of expressions, illustrated in bold below:您已经看到了下面用粗体显示的表达式示例:
int cadence = 0; anArray[0] = 100; System.out.println("Element 1 at index 0: " + anArray[0]); int result = 1 + 2; // result is now 3 if (value1 == value2) System.out.println("value1 == value2");
The data type of the value returned by an expression depends on the elements used in the expression.表达式返回的值的数据类型取决于表达式中使用的元素。The expression 表达式cadence = 0
returns an int
because the assignment operator returns a value of the same data type as its left-hand operand; in this case, cadence
is an int
.cadence=0
返回一个int
,因为赋值运算符返回与其左操作数相同数据类型的值;在本例中,cadence
是一个int
。As you can see from the other expressions, an expression can return other types of values as well, such as 从其他表达式可以看出,表达式也可以返回其他类型的值,例如boolean
or String
.boolean
或String
。
The Java programming language allows you to construct compound expressions from various smaller expressions as long as the data type required by one part of the expression matches the data type of the other.Java编程语言允许您从各种较小的表达式构造复合表达式,只要表达式的一部分所需的数据类型与另一部分的数据类型匹配。Here's an example of a compound expression:下面是一个复合表达式的示例:
1 * 2 * 3
In this particular example, the order in which the expression is evaluated is unimportant because the result of multiplication is independent of order; the outcome is always the same, no matter in which order you apply the multiplications.在此特定示例中,表达式的求值顺序不重要,因为乘法结果与顺序无关;结果总是一样的,无论您以何种顺序应用乘法。However, this is not true of all expressions.然而,并非所有的表达都是如此。For example, the following expression gives different results, depending on whether you perform the addition or the division operation first:例如,根据您是先执行加法运算还是先执行除法运算,以下表达式会给出不同的结果:
x + y / 100 // ambiguous
You can specify exactly how an expression will be evaluated using balanced parenthesis: ( and ).可以使用平衡圆括号(
和)
精确指定表达式的求值方式。For example, to make the previous expression unambiguous, you could write the following:例如,要使前面的表达式明确,可以编写以下代码:
(x + y) / 100 // unambiguous, recommended
If you don't explicitly indicate the order for the operations to be performed, the order is determined by the precedence assigned to the operators in use within the expression.如果未明确指示要执行的操作的顺序,则顺序由表达式中指定给正在使用的运算符的优先级决定。Operators that have a higher precedence get evaluated first.具有较高优先级的运算符将首先求值。For example, the division operator has a higher precedence than does the addition operator.例如,除法运算符的优先级高于加法运算符。Therefore, the following two statements are equivalent:因此,以下两种说法是等效的:
x + y / 100
x + (y / 100) // unambiguous, recommended
When writing compound expressions, be explicit and indicate with parentheses which operators should be evaluated first.在编写复合表达式时,要明确,并用括号指明应首先计算哪些运算符。This practice makes code easier to read and to maintain.这种做法使代码更易于阅读和维护。
Statements are roughly equivalent to sentences in natural languages.语句大致相当于自然语言中的句子。A statement forms a complete unit of execution.语句构成完整的执行单元。The following types of expressions can be made into a statement by terminating the expression with a semicolon (通过使用分号(;
).;
)终止表达式,可以将以下类型的表达式生成语句。
++
or --
++
或--
Such statements are called expression statements.这样的语句称为表达式语句。Here are some examples of expression statements.下面是一些表达式语句的示例。
// assignment statement aValue = 8933.234; // increment statement aValue++; // method invocation statement System.out.println("Hello World!"); // object creation statement Bicycle myBike = new Bicycle();
In addition to expression statements, there are two other kinds of statements: declaration statements and control flow statements.除了表达式语句,还有两种其他类型的语句:声明语句和控制流语句。A declaration statement declares a variable.声明语句声明一个变量。You've seen many examples of declaration statements already:您已经看到了许多声明语句的示例:
// declaration statement double aValue = 8933.234;
Finally, control flow statements regulate the order in which statements get executed.最后,控制流语句控制语句的执行顺序。You'll learn about control flow statements in the next section, Control Flow Statements您将在下一节,控制流语句中了解控制流语句
A block is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed.块是平衡大括号之间的一组零或多个语句,可以在允许使用单个语句的任何地方使用。The following example, 以下示例BlockDemo
, illustrates the use of blocks:BlockDemo
演示了块的使用:
class BlockDemo { public static void main(String[] args) { boolean condition = true; if (condition) { // begin block 1 System.out.println("Condition is true."); } // end block one else { // begin block 2 System.out.println("Condition is false."); } // end block 2 } }