Expressions and Output · 表达式与输出
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| expression/ekˈspreʃn/ | 表达式 | biǎo dá shì |
| operators/ˈɒpəreɪtəz/ | 运算符 | yùn suàn fú |
| modulus/ˈmɒdjʊləs/ | 取模 | qǔ mó |
| concatenates/kənˈkætəneɪts/ | 拼接 | pīn jiē |
Arithmetic expressions
- An expression 表达式 is a piece of code that computes a value.
- Java's arithmetic operators 运算符:
+,-,*,/, and%(remainder). %is the modulus 取模 operator:17 % 5is2(the leftover after dividing).- Expressions follow the usual order of operations (
*,/,%before+,-).
算术表达式
- 表达式是一段计算出值的代码。
- Java 的算术运算符:
+、-、*、/和%(求余)。 %是取模运算符:17 % 5是2(除完后剩下的)。- 表达式遵循通常的运算顺序(
*、/、%先于+、-)。
Precedence and grouping
- Multiplication and division bind tighter than addition and subtraction.
- Use parentheses to force the order you want:
(a + b) * c. - Operators of equal precedence evaluate left to right.
- When in doubt, add parentheses — clarity beats cleverness.
优先级与分组
- 乘除的结合力比加减更紧。
- 用圆括号强制你想要的顺序:
(a + b) * c。 - 优先级相同的运算符从左到右求值。
- 拿不准时就加括号——清晰胜过取巧。
Printing output
System.out.println(x)printsxand moves to a new line.System.out.print(x)printsxwith no new line after.- The
+operator concatenates 拼接 strings:"Score: " + score. - Mixing a String with a number in
+turns the number into text.
打印输出
System.out.println(x)打印x并换到新的一行。System.out.print(x)打印x,之后不换行。+运算符拼接字符串:"Score: " + score。- 在
+中把 String 和数字混用,会把数字变成文本。
String concatenation vs. addition
- If either side of
+is a String,+means concatenation, not addition. "" + 3 + 4gives"34"(text), but3 + 4 + ""gives"7"(added first).- Evaluation is left to right, so the first
+decides the mode. - Wrap numeric math in parentheses when concatenating:
"Sum: " + (3 + 4).
字符串拼接对加法
- 如果
+的任一侧是 String,+就表示拼接,而非加法。 "" + 3 + 4得到"34"(文本),但3 + 4 + ""得到"7"(先相加)。- 求值是从左到右的,所以第一个
+决定了模式。 - 拼接时把数值运算用括号包起来:
"Sum: " + (3 + 4)。
+ is overloaded: it adds numbers but concatenates Strings, left to right. "Total: " + 3 + 4 prints Total: 34 (each + hits a String), while "Total: " + (3 + 4) prints Total: 7. And % gives the remainder — great for "is it even?" (n % 2 == 0) — don't confuse it with division.
+ 被重载了:它对数字做加法,对 String 做拼接,从左到右。"Total: " + 3 + 4 打印 Total: 34(每个 + 都碰到 String),而 "Total: " + (3 + 4) 打印 Total: 7。而 % 给出余数——很适合判断“是否为偶数”(n % 2 == 0)——别把它和除法弄混。
Evaluate 10 + 2 * 3:
*first:2 * 3is6.- Then
+:10 + 6is16. System.out.println("Answer: " + 16)printsAnswer: 16.
求 10 + 2 * 3:
- 先
*:2 * 3是6。 - 再
+:10 + 6是16。 System.out.println("Answer: " + 16)打印Answer: 16。
An expression computes a value using operators + - * / % under standard precedence (* / % before + -, left to right, parentheses override). % gives the remainder. Print with System.out.println (newline) or print (none); + concatenates when a String is involved — parenthesize numeric math.
表达式用运算符 + - * / % 在标准优先级下计算一个值(* / % 先于 + -,从左到右,括号可覆盖)。% 给出余数。用 System.out.println(换行)或 print(不换行)打印;当涉及 String 时 + 做拼接——把数值运算加括号。
Evaluating an expression step by step · 逐步求值一个表达式
- binds tighter than +, so 23 happens first. · 比 + 结合更紧,所以先算 2*3。
Evaluate the Java expression 10 + 2 * 3. · 求 Java 表达式 10 + 2 * 3 的值。
- first (2*3=6), then + (10+6=16). · 先 (23=6),再 +(10+6=16)。
What is 17 % 5 in Java (the remainder)? · 在 Java 中 17 % 5(余数)是多少?
17 = 3×5 + 2, so the remainder is 2. · 17 = 3×5 + 2,所以余数是 2。
What does System.out.println("Total: " + 3 + 4) print? · System.out.println("Total: " + 3 + 4) 打印什么?
Left to right, each + hits a String → concatenation → 34. · 从左到右,每个 + 都碰到 String → 拼接 → 34。
The operator that gives the remainder of a division is ___ (one symbol). · 给出除法余数的运算符是 ___(一个符号)。
% is the modulus (remainder) operator. · % 是取模(求余)运算符。
System.out.print(x) moves to a new line after printing. · System.out.print(x) 打印后会换到新的一行。
print stays on the same line; println adds the newline. · print 停在同一行;println 才加换行。