英文:
question about operator in java with System.out.print
问题
以下是翻译后的内容:
有人可以告诉我其中的区别以及为什么吗?我有一个示例:
int a = 2;
int b = 7;
System.out.println(a + b);
System.out.println(a + b + "String");
System.out.println("String" + a + b + "String");
System.out.println(a + b + "String" + a + b + "String");
System.out.println("String" + a + b + "String" + a + b);
输出结果:
9
9String
String27String
9String27String
String27String27
英文:
Can anyone tell me the difference and why ????????
I have an example:
int a = 2;
int b = 7;
System.out.println(a+b);
System.out.println(a+b+"String");
System.out.println("String"+a+b+"String");
System.out.println(a+b+"String"+a+b+"String");
System.out.println("String"+a+b+"String"+a+b);
output
9
9String
String27String
9String27String
String27String27
答案1
得分: 2
要理解正在发生的事情,请考虑在Java中这些表达式是左结合的。添加int
+ int
的结果是两个值的算术和。将int
与String
相加的结果相当于将int
转换为其String
表示,然后在这两个String
之间执行+
操作,将这两个值连接在一起。因此,
a+b+"String" => (a + b) + "String"
=> (2 + 7) + "String"
=> 9 + "String"
=> "9String"
a+b+"String"+a+b+"String" => ((((a + b) + "String") + a) + b) + "String"
=> ((((2 + 7) + "String") + 2) + 7) + "String"
=> (((9 + "String") + 2) + 7) + "String"
=> (("9String" + 2) + 7) + "String"
=> ("9String2" + 7) + "String"
=> "9String27" + "String"
=> "9String27String"
英文:
To understand what's going on, consider that these expressions in Java are left-associative. The result of adding int
+ int
is the arithmetic sum of the two values. The result of adding an int
with a String
is equivalent to converting the int
to its String
representation and then performing the +
operation between the two String
s, which concatenates the two values. Thus,
a+b+"String"` => (a + b) + "String"
=> (2 + 7) + "String"
=> 9 + "String"
=> "9String"
a+b+"String"+a+b+"String"` => ((((a + b) + "String") + a) + b) + "String"
=> ((((2 + 7) + "String") + 2) + 7) + "String"
=> (((9 + "String") + 2) + 7) + "String"
=> (("9String" + 2) + 7) + "String"
=> ("9String2" + 7) + "String"
=> "9String27" + "String"
=> "9String27String"
答案2
得分: 1
判断+
表示加法还是连接的过程如下:
-
根据标准的运算符优先规则,确定运算符的应用顺序。
-
检查每个
+
。如果+
的左右操作数表达式都是基本的或包装过的数值类型,那么+
表示加法。否则表示字符串连接。
示例:
将优先规则应用于
a+b+"String"+a+b+"String"
意味着它等同于
((((a+b)+"String")+a)+b)+"String"
然后:
a+b
是加法(a+b)+"String")
是连接((a+b)+"String")+a
是连接(((a+b)+"String")+a)+b
是连接((((a+b)+"String")+a)+b)+"String"
是连接
这与你在该情况下得到的输出 9String27String
相符。
英文:
Procedure to determine whether +
means addition or concatenation:
-
Apply standard operator precedence rules to the expression to work out the order in which the operators are applied.
-
Examine each
+
. If both left and right operand expressions of+
are primitive or wrapped numeric, then the+
is an addition. Otherwise it is string concatenation.
Example:
Applying precedence rules to
a+b+"String"+a+b+"String"
means that it is equivalent to
((((a+b)+"String")+a)+b)+"String"
Then:
a+b
is addition(a+b)+"String")
is concatenation((a+b)+"String")+a
is concatenation(((a+b)+"String")+a)+b
is concatenation((((a+b)+"String")+a)+b)+"String"
is concatenation
This matches up with the output 9String27String
that you got for that case.
答案3
得分: 0
代码部分已翻译如下:
System.out.println("String" + a + b + "String");
假设你想要进行String
连接。
你可以这样做:
System.out.println("String" + (a + b) + "String");
或者
System.out.printf("String%dString%n", a + b);
英文:
The code
System.out.println("String"+a+b+"String");
is assuming that you want to do String
concatenation.
You could do
System.out.println("String"+(a+b)+"String");
or
System.out.printf("String%dString%n", a+b);
答案4
得分: 0
在Java中,加号可以用于执行算术加法。
它还可以用于连接字符串。当加号以这种方式使用时,右侧的操作数在与左侧的操作数连接之前会自动转换为字符字符串。
英文:
In Java, the plus sign can be used to perform arithmetic addition.
It can also be used to concatenate strings. When the plus sign is used in this manner, the operand on the right is automatically converted to a character string before being concatenated with the operand on the left.
答案5
得分: 0
你最好查阅Java规范中的**字符串连接运算符 +**,你会看到类似下面的说明,这将给你答案。
英文:
You had better check the String Concatenation Operator + from java specification, you will see the instructions like below,which will give you the answer
答案6
得分: -2
请首先理解字符串中的“+”的含义。
英文:
Please understand the meaning of "+" in string first
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论