简体   繁体   English

字符串如何接受整数?

[英]How can a string accept an integer?

Can a variable String accept integer value as well. 变量String也可以接受整数值。 Or can we concat integer with a string ? 或者我们可以用字符串连接整数?

Example: 例:

public class TestString1 {
        public static void main(String[] args) {
        String str = "420";
        str += 42;
        System.out.print(str);
    }
}

I was expecting compilation error over here because String was getting concatenated with an integer. 我期待编译错误,因为String正在与一个整数连接。

JLS documentation on String concatination operator( + )- 字符串连接运算符( + )上的JLS文档 -

15.18.1 String Concatenation Operator + 15.18.1字符串连接运算符+

If only one operand expression is of type String, then string conversion is performed on the other operand to produce a string at run time. 如果只有一个操作数表达式是String类型,则在另一个操作数上执行字符串转换以在运行时生成字符串。 The result is a reference to a String object (newly created, unless the expression is a compile-time constant expression (§15.28))that is the concatenation of the two operand strings. 结果是对String对象的引用(新创建,除非表达式是编译时常量表达式(第15.28节)),它是两个操作数字符串的串联。 The characters of the left-hand operand precede the characters of the right-hand operand in the newly created string. 左侧操作数的字符位于新创建的字符串中右侧操作数的字符之前。 If an operand of type String is null, then the string "null" is used instead of that operand 如果String类型的操作数为null,则使用字符串“null”而不是该操作数

That is why String + int does not produce any error. 这就是String + int不会产生任何错误的原因。 And it prints 42042 它打印42042

None of the other answers have explained what's actually being executed here. 其他答案都没有解释这里实际执行的内容。

Your code will be converted to something like: 您的代码将转换为以下内容:

String str = "420";

// str += 42;
StringBuilder sb = new StringBuilder(str);
sb.append(42);  // which internally does something similar to String.valueOf()
str = sb.toString();

System.out.print(str);

int添加到String会将int附加到字符串,从而将int转换为字符串。

Anything that is given in double quotes is String and + is for concatenating string with an any value(int value). 双引号中给出的任何内容都是String, +用于将字符串与任意值(int值)连接。
So the integer value will be appended to the string. 因此整数值将附加到字符串。 Here 这里

String str = "420";
     str += 42; // 42 will be appended to 420 and result will be 42042

x=20 y=10 x = 20 y = 10

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

B - Bracket O - Power DM - Division and Multiplication AS - Addition and Substraction B - 支架O - 功率DM - 分频和乘法AS - 加法和减法

This works from Left to Right if the Operators are of Same precedence 如果运算符具有相同的优先级,则从左到右工作

Now 现在

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

"printing: " : Is a String" “打印:”:是一个字符串“

"+" : Is the only overloaded operator in Java which will concatenate Number to String. “+”:是Java中唯一一个将Number连接到String的重载运算符。 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在“打印:”+之后已经发生了,因此它将x和y视为字符串。

So the output is 2010. 所以产量是2010年。

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

Here the 在这里

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

So its x*y first then printing: + 所以它的x * y首先打印:+

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)); System.out.println(“printing:”+(x + y));

The Order of precedence of Bracket is higher to Addition. Bracket的优先顺序高于Addition。

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

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