简体   繁体   English

连接字符串和数字 Java

[英]concatenating string and numbers Java

Why is the output different in these cases?为什么 output 在这些情况下有所不同?

int x=20,y=10;

System.out.println("printing: " + x + y); ==> printing: 2010 ==>印刷:2010

System.out.println("printing: " + x * y); ==> printing: 200 ==>打印:200

Why isn't the first output 30?为什么第一个 output 不是 30? Is it related to operator precedence?它与运算符优先级有关吗? Like first "printing" and x are concatenated and then this resulting string and y are concatenated?就像第一个“打印”和 x 被连接,然后这个结果字符串和 y 被连接? Am I correct?我对么?

Its the BODMAS Rule 它是BODMAS规则

I am showing the Order of precedence below from Higher to Low: 我在下面显示从高到低的优先顺序:

B  - Bracket 
O  - Power
DM - Division and Multiplication
AS - Addition and Substraction

This works from Left to Right if the Operators are of Same precedence 该作品由 Left to Right 如果运营商是相同的优先级的

Now 现在

System.out.println("printing: " + x + y);

"printing: " : Is a String" "printing: " :是字符串”

"+" : Is the only overloaded operator in Java which will concatenate Number to String. "+" :是Java中唯一将数字连接到字符串的重载运算符。 As we have 2 "+" operator here, and x+y falls after the "printing:" + as already taken place, Its considering x and y as Strings too. 因为我们这里有2个“ +”运算符,并且x + y落在"printing:" +正如已经发生的那样,它也将x和y视为字符串。

So the output is 2010. 因此输出为2010。

System.out.println("printing: " + x * y);

Here the 在这里

"*" : Has higher precedence than + "*" :优先级高于+

So its x*y first then printing: + 因此,它的x*y 首先 printing: +

So the output is 200 所以输出是200

Do it like this if you want 200 as output in first case: 如果要在第一种情况下将200作为输出,请执行以下操作:

System.out.println("printing: "+ (x+y));

The Order of precedence of Bracket is higher to Addition . Bracket 的优先级 高于 Addition

Basic math tells you that adding numbers is done each at a time. 基本数学告诉您,每次都进行加法运算。

So "printing: " + x is computed first. 因此, "printing: " + x是首先计算的。 As it sa string + int the result is "printing: 20" . 因为它是string + int ,结果为"printing: 20" Then you add y so "printing: 20" + y equals "printing: 2010" . 然后添加y因此"printing: 20" + y等于"printing: 2010"

In the second case, multiplying is prioritary. 在第二种情况下,乘法是优先的。 So first x * y is calculated and equals 200 . 因此,首先计算x * y等于200 Then "printing: " + 200 equals "printing: 200" . 然后"printing: " + 200等于"printing: 200"

The results that you observe are certainly related to operator precedence and also the order of evaluation . 您观察到的结果当然与运算符优先级以及评估顺序有关 In the absence of another rule, ie an operator of higher precedence, operators are evaluated in order from left to right. 在没有其他规则(即优先级较高的运算符)的情况下,将按从左到右的顺序评估运算符。

In the first expression, all operators have the same precedence, because they're the same operator: + , and so the first operation is evaluated. 在第一个表达式中,所有运算符具有相同的优先级,因为它们是相同的运算符: + ,因此对第一个运算进行求值。 Since it involves a String , it is String concatenation, and the result is a String ; 由于它涉及String ,因此它是String串联,结果是String similarly for the following + . 对于以下+也类似。

In the second expression, one of the operators is * , which has higher precedence than + , and so is evaluated first. 在第二个表达式中,运算符之一是* ,其优先级高于+ ,因此首先被求值。 You get the result of the multiplication, an integer, and then the concatenation of a String and an int due to the + . 你得到的乘法,一个整数, 然后的连接结果Stringint由于+

This will print 30 : 这将打印30

System.out.println("printing: " + (x + y))

You need the parentheses to express the precedence you wish for. 您需要括号来表达您想要的优先级。

It is for operator precedence 用于操作员优先

System.out.println("printing: " + x * y);

here * operator has more precedence than + so first it calculate x * y . 这里*运算符的优先级比+高,因此首先计算x * y

System.out.println("printing: " + x + y);

Where as here all are same operator and it will be treated as String concatenation operation as there is one string value is there. 这里所有都是相同的运算符,并且由于存在一个string值,因此它将被视为字符串连接操作。

if you involve bracket into this - System.out.println("printing: " + (x + y)); 如果您将括号包含在其中-System.out.println(“ printing:” +(x + y));

Then bracket ( operator has more precedence than + so first it will calculate (x + y) and will print 30 然后括号(运算符的优先级比+高,因此首先将计算(x + y)并输出30

check detail operator precedence order 检查详细运算符的优先顺序

In the first printing line, the + operator is applied first between the String and the int and the result is a String which is again concatenated with another int resulting a String. 在第一行打印中,首先在String和int之间应用+运算符,结果是一个String,该字符串再次与另一个int串联在一起,生成一个String。

In the second line, the order of the operations is: first the multiplication and the resulted int concatenated to the String. 在第二行中,运算的顺序为:首先是乘法,结果int连接到String。

System.out.println("2*("+a"+"+b"*"+c")")

如何打印

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM