简体   繁体   English

如何在java中将两个或多个数字添加到字符串中?

[英]How to add the two or more numbers in to string in java?

  1. We can't add the numbers in to string data type with out the string value,it will not compile我们不能将数字添加到没有字符串值的字符串数据类型中,它不会编译
String val = 5+5;
  1. We can add two or more numbers before appending the string.我们可以在附加字符串之前添加两个或多个数字。
String val = 10 + 10 + " Hello "
System.out.println(val);

Output输出

20 Hello
  1. Adding two numbers,append the value to the number but After appending the string value we can't add two or more numbers添加两个数字,将值附加到数字但附加字符串值后我们不能添加两个或更多数字
public class Display {    
    public static void main(String[] args) {
        String val = 10 + 10 + " Hello " + 20 + 10;
        System.out.println(val);
    }    
}

Output :输出 :

20 Hello 2010

can anyone explain this?谁能解释一下?

It's String concatenation.这是字符串连接。 As soon as there is a String added, all the next elements will be added to the String, not as an int.一旦添加了一个字符串,所有下一个元素都将添加到字符串中,而不是作为一个整数。

String a = 10 + 5 + " result";

Does this:做这个:

10 + 5 + " result";
15 + " result";
"15 result";

String a = 10 + " result " + 5 + 2;

becomes:变成:

"10 result " + 5 + 2;
"10 result 5" + 2;
"10 result 52"; 

Make sure the ints are added before concatenation:确保在串联之​​前添加整数:

String a = 10 + " result " + (5 + 2);

EDIT: As for the编辑:至于

String val = 5 + 5;

this will cause problems because both elements are ints.这会导致问题,因为两个元素都是整数。 What you are doing here is not concatenating, but adding two ints, which results in another int.您在这里所做的不是连接,而是添加两个整数,从而产生另一个整数。

The explanation I see is the following: the "plus" operator is defined as follows:我看到的解释如下:“加号”运算符定义如下:

  • int + int -> int int + int -> int
  • int + String -> String int + 字符串 -> 字符串
  • String + int -> String字符串 + 整数 -> 字符串

You then have the following line: ((((int + int) + String) + int) + int) Resolving the operation one after the other, you end with the following operations to resolve:然后您有以下行: ((((int + int) + String) + int) + int) 一个接一个地解析操作,以以下操作结束:

  • int(10) + int(10) -> int(20) int(10) + int(10) -> int(20)
  • int(20) + String( Hello ) -> String (20 Hello) int(20) + String( 你好) -> String (20 你好)
  • String (20 Hello) + int(20) -> String (20 Hello 20)字符串 (20 你好) + int(20) -> 字符串 (20 你好 20)
  • String (20 Hello 20) + int(10) -> String (20 Hello 2010)字符串 (20 你好 20) + int(10) -> 字符串 (20 你好 2010)
  1. You can't do it like that because 5+5 will return an integer, therefore you can't assign it into string data type你不能那样做,因为 5+5 将返回一个整数,因此你不能将它分配给字符串数据类型

  2. If you append 10+10 then append it with string, it'll return a string, so you can assign it to string如果附加 10+10 然后附加字符串,它将返回一个字符串,因此您可以将其分配给字符串

  3. The first 10+10 still count as integer then you append it with string => it'll return to string第一个 10+10 仍然算作整数,然后用字符串附加它 => 它会返回到字符串

then you add it with integer 20 => return as string that string will return string if you add it with more and more integer然后你用整数 20 添加它 => 作为字符串返回,如果你用越来越多的整数添加它,该字符串将返回字符串

you can try like this你可以这样试试

public class Display {    
    public static void main(String[] args) {
        String val = 10 + 10 + " Hello " + (20 + 10);
        System.out.println(val);
    }    
}

output输出

20 Hello 30 20 你好 30

because after string all next element will be concatenation因为在字符串之后所有下一个元素都将被连接

If you put parenthesis before appending to string it will work.如果在附加到字符串之前加上括号,它将起作用。

 public static void main(String[] args) {
    String val = 10 + 10 + " Hello " +( 20 + 10);
    System.out.println(val);
}  

Order of operations matters, also knowing how such a statement is evaluated behind the screen.操作顺序很重要,也知道如何在屏幕后面评估这样的语句。 Check out the below rough description of how your code is evaluated.查看以下有关如何评估您的代码的粗略描述。

multiplying and division before addition and substraction.加法和减法之前的乘法和除法。

in this code:在这段代码中:

String val = 10 + 10 + " Hello " + 20 + 10;

it's evaluated like:它的评估如下:

Check operations for multiplications and division
Nothing found
Check for additions.
Integer found.
Addition found
Integer found
Add integer to integer. 
Remember integer.
Addition found
String found.
Create string builder.
Add remembered integer to string builder
Add string to stringbuilder
Addition found
integer found
add integer to string builder
Addition found
integer found
add integer to string builder
return string from string builder.

If you'd have an addition, that would be executed first:如果您有添加,则将首先执行:

https://ideone.com/aPlaPv https://ideone.com/aPlaPv

String val = 10 + 10 + " Hello " + 20 * 10;

It's evaluated like:它的评估如下:

Check operations for multiplication and division
Multiplication found
Integer found
Multiplication found
Integer found
Remember multiplication
no more multiplications found
Return to start
Check for additions.
Integer found.
Addition found
Integer found
Add integer to integer. 
Remember integer.
Addition found
String found.
Create string builder.
Add remembered integer to string builder
Add string to stringbuilder
add remembered multiplication result to string builder
return string from string builder

These simple questions are confusing me.这些简单的问题让我很困惑。 It's simple math with priority.这是具有优先级的简单数学。 when we try to concatenate number with number it will be number if one of provided is string then it will be transformed to string.当我们尝试将数字与数字连接起来时,如果提供的其中之一是字符串,它将是数字,那么它将被转换为字符串。

String val = 10 + 10 + " Hello " + 20 + 10;字符串 val = 10 + 10 + "你好" + 20 + 10;

   number  =>   string  => string => string   -> result String

((((number + number) + string) + number) + number) (((((number + number) + string) + number) + number)

The problem is the associativity of operand in java问题是java中操作数的结合性

The "+" operand in java have an associativity from left to right , so this means that the expression 10+10 is the first to evalutate and because these are integers (10,10) the result is integer (20). Java 中的“+”操作数具有从左到右的结合性,因此这意味着表达式 10+10 是第一个求值的,并且因为这些是整数 (10,10),所以结果是整数 (20)。
Then it evalutate 20 + " hello ", because they are an integer and a string the java compiler transform the 20 into a string.然后它评估 20 + " hello ",因为它们是一个整数和一个字符串,java 编译器将 20 转换为一个字符串。 Now string + string expression produces a string "20 hello ".现在字符串 + 字符串表达式产生一个字符串“20 hello”。

Now is the problem现在是问题

at this point,because "20 hello " is a string, the expression "20 hello" + 20 transform 20 into a string and the result is "20 hello 20".When is added the last term (10) it is converted into a string and it is concatenated to the string "20 hello 20" making "20 hello 2010".此时,因为“20 hello”是一个字符串,表达式“20 hello”+ 20将20转换为一个字符串,结果为“20 hello 20”。当加上最后一项(10)时,它被转换为一个字符串,它被连接到字符串“20 hello 20”,形成“20 hello 2010”。

to avoid this inconvenient you can place round brackets in the expression,为避免这种不便,您可以在表达式中放置圆括号,
20 + " hello "+ ( 20 + 10 )

The problem is that the value you are passing to the string in concatenating.问题是您在连接时传递给字符串的值。 You need to specify where to look specifically.您需要指定具体查看的位置。

The best approach will be to leave the addition values in brackets.最好的方法是将附加值保留在括号中。

String val = (10 + 10) + " Hello" + (20 + 10);

or或者

String val = 10 + 10 + " Hello" + (20 + 10);

I would suggest using brackets where possible to avoid getting these small errors when the compiler compiles the code.我建议尽可能使用括号以避免在编译器编译代码时出现这些小错误。

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

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