Why can I add characters to strings but not characters to characters?

谁说我不能喝 提交于 2019-11-28 09:58:09

问题


So I wanted to add a character to a string, and in some cases wanted to double that characters then add it to a string (i.e. add to it itself first). I tried this as shown below.

char s = 'X'; 
String string = s + s;

This threw up an error, but I'd already added a single character to a string so I tried:

String string = "" + s + s;

Which worked. Why does the inclusion of a string in the summation cause it to work? Is adding a string property which can only be used by characters when they're converted to strings due to the presence of a string?


回答1:


It's because String + Char = String, similar to how an int + double = double.

Char + Char is int despite what the other answers tell you.

String s = 1; // compilation error due to mismatched types.

Your working code is (String+Char)+Char. If you had done this: String+(Char+Char) you would get a number in your string. Example:

System.out.println("" + ('x' + 'x')); // prints 240
System.out.println(("" + 'x') + 'x'); // prints xx - this is the same as leaving out the ( ).



回答2:


char + char returns an int so the compiler complains that String string = (int), which is indeed wrong.

To concatenate the chars, you can use the empty String ("") at the beginning so the + operator will be for String concatenation or use a StringBuilder that can append chars as well.

char s = 'X';
String string = new StringBuilder().append(s).append(s).toString();

Note: the char variable is s, not X.




回答3:


In Java, char is a primitive integral numeric type. As such, the operator + is defined to mean addition of two chars, with an int result. The operator means concatenation only on strings. So one way to do it is

"" + char1 + char2

This will coerce the right-hand operand to a string, which is what you want to achieve.

A side point: char is the only unsigned primitive numeric type in Java and in one project of mine I specifically use it as such.




回答4:


When you do char s = 'X'; String string = X + X;

Why not do this instead? char s = 'X'; String string = s + s;




回答5:


Adding in the "" changes the return type to a string. Leaving it out means the return type is a char which doesn't match.




回答6:


String string = "" + X + X;

is an example of concatenation. By pre-pending the empty string you specify that the result should be a string. The first line of code tells the compiler that you are adding 2 chars (or ints) which should result in an int, and not a string.




回答7:


 String string = X + X;

Here X i'ts threated as a variable

You should use something as

String string ="x x";

Or

String x = "something";
String y = "else";

then String string= x+y; should work just fine, this is because you are concatening with the "+" sign, you could also use

string = string.concat(x+y); 

or

string = string.concat("something"+"else");


来源:https://stackoverflow.com/questions/18217005/why-can-i-add-characters-to-strings-but-not-characters-to-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!