Java:你能解释一下这个简单的语句(System.out.println)吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2361789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:34:05  来源:igfitidea点击:

Java: Could you explain this simple statement (System.out.println)?

java

提问by pele

System.out.println(1 + 2 + "3");

Output: 33

输出:33

System.out.println("1" + 2 + 3);

Output: 123

输出:123

采纳答案by animuson

Well, it's a thing called order of operations.

嗯,这是一个叫做操作顺序的东西。

1 + 2 is calculated to equal 3 and then the string "3" is appended to it converting the first 3 to a string and printing "33".

1 + 2 计算为等于 3,然后将字符串“3”附加到它,将前 3 转换为字符串并打印“33”。

In your second instance, "1" is already a string so adding numbers will convert them to strings to match, so appending "2" and then appending "3" and printing "123".

在您的第二个实例中,“1”已经是一个字符串,因此添加数字会将它们转换为字符串以匹配,因此附加“2”然后附加“3”并打印“123”。

P.S. Strings take precedence because they have a higher casting priority than integers do, therefore it will convert integers to strings but not strings to integers, as with the second example.

PS 字符串优先,因为它们比整数具有更高的转换优先级,因此它将整数转换为字符串,但不会将字符串转换为整数,就像第二个示例一样。

回答by Aurril

The first statement adds 1 and 2 (since both are Integers) and then converts them to a string and appends the string "3".

第一条语句将 1 和 2(因为两者都是整数)相加,然后将它们转换为字符串并附加字符串“3”。

The second statement has a string "1" and converts all following arguments to strings as well. So you get 123.

第二个语句有一个字符串“1”,并将所有后面的参数也转换为字符串。所以你得到123。

回答by codaddict

In the case of 1 + 2 + "3"

如果是 1 + 2 + "3"

Addition of 1 and 2 is performed first next 3 is concatenated to 3.

首先执行 1 和 2 的加法,然后将 3 连接到 3。

In "1" + 2 + 3

"1" + 2 + 3

1 is concatenated to 2 and the result ("12") is concatenated to 3

1 连接到 2,结果(“12”)连接到 3

The thing to remember is:

要记住的事情是:

If either of the operands to + is a string + acts as concatenation else it works as addition.

如果 + 的任一操作数是字符串 + 充当连接,否则它充当加法。

回答by Pavunkumar

In case of first , it does 1+2 , then it does the string concatenation operation , So that it gives you 33.

在 first 的情况下,它执行 1+2 ,然后执行字符串连接操作,因此它为您提供 33。

In case of second statement it is doing string concatenation for all operand , since first

在第二个语句的情况下,它正在对所有操作数进行字符串连接,因为第一个

operand is string. So that it gives you 123

操作数是字符串。所以它给你 123

回答by f4.

I'm not a java expert but I suppose expressions are read from left to right here

我不是 Java 专家,但我想这里的表达式是从左到右阅读的

In the first case it first compute 1 + 3 which gives 3 then 3 + "3" which convert the first 3 to a string and gives "33"

在第一种情况下,它首先计算 1 + 3 给出 3 然后 3 + "3" 将前 3 转换为字符串并给出 "33"

In the second case it starts by "1" + 2 which gives "12" and then "12" + 3 = "123"

在第二种情况下,它以 "1" + 2 开头,给出 "12" 然后 "12" + 3 = "123"

This is a side effect of having an operator + which concatenates 2 strings and an other which adds 2 numbers.

这是具有连接 2 个字符串和另一个添加 2 个数字的运算符 + 的副作用。

回答by Aman Kasliwal

The compiler will convert an operand to its string equivalent whenever the other operand of the +is an instance of String.

只要+的另一个操作数是String的实例,编译器就会将一个操作数转换为其等效的字符串

For the second case:Operator precedence causes the concatenation of "1" with the string equivalent of 2 to take place first. This result is then concatenated with the string equivalent of 3 a second time.

对于第二种情况:运算符优先级导致“1”与等效于 2 的字符串的串联首先发生。然后将该结果与等效于 3 的字符串再次连接。

To complete the integer addition first, you must use parentheses, like this:

要先完成整数加法,必须使用括号,如下所示:

System.out.println("1" + (2 + 3));

For the first case:Operator precedence will first add 1 and 2. Now,string equivalent of this result is then concatenated with "3".

对于第一种情况:运算符优先级将首先添加 1 和 2。现在,该结果的等效字符串然后与“3”连接。

回答by assylias

This is defined by the Java Language Specification #15.18.1

这是由Java 语言规范 #15.18.1定义的

The + operator is syntactically left-associative, no matter whether it is determined by type analysis to represent string concatenation or numeric addition. In some cases care is required to get the desired result. For example, the expression:
a + b + c
is always regarded as meaning:
(a + b) + c

+ 运算符在语法上是左结合的,无论是通过类型分析确定表示字符串连接还是数字加法。在某些情况下,需要小心才能获得所需的结果。例如,表达式:
a + b + c
始终被视为含义:
(a + b) + c

Then for each subgroup (a+b)(let's call it x) and (x + c):

然后对于每个子组(a+b)(我们称之为x)和(x + c)

If only one operand expression is of type String, then string conversion (§5.1.11) is performed on the other operand to produce a string at run-time.

如果只有一个操作数表达式是字符串类型,则在运行时对另一个操作数执行字符串转换(第 5.1.11 节)以生成字符串。

回答by varda

system.out.println(-1>>>1);

system.out.println(-1>>>1);

For the second case: Operator precedence causes the concatenation of "1" with the string equivalent of 2 to take place first. This result is then concatenated with the string equivalent of 3 a second time.

对于第二种情况:运算符优先级导致“1”与等效于 2 的字符串的串联首先发生。然后将该结果与等效于 3 的字符串再次连接。

To complete the integer addition first, you must use parentheses, like this:

要先完成整数加法,必须使用括号,如下所示:

回答by Codecracker

In 1st case 1 and 2 treated as integer so the answer is 3 and next + concatenate the result with string 3. In 2nd case "1" is a string and sop treat every thing as string after appearing a string ie "2" and "3" also act as strings so all get concatenate to show output 123

在第一种情况下,1 和 2 被视为整数,所以答案是 3,然后将结果与字符串 3 连接起来。在第二种情况下,“1”是一个字符串,sop 在出现字符串后将所有东西都视为字符串,即“2”和“ 3" 也充当字符串,因此所有字符串都连接起来以显示输出 123