java 字符串字符连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17801246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String character concatenation
提问by Abhinav
I was writing a piece of code to reverse a String wordwise(Input:Java is fun; Output:fun is Java), when I came across a question:How can a String variable be added to a character type variable.the code is working perfectly, but how is this possible? Someone, please explain this to me. Thanks in advance.
我正在写一段代码来逐字反转字符串(输入:Java 很有趣;输出:fun 是 Java),当我遇到一个问题:如何将字符串变量添加到字符类型变量中。代码正在运行完美,但这怎么可能?有人请给我解释一下。提前致谢。
import java.io.*;
public class numberof {
public static void main(String args[])throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str1,str2="",str3="";
str1=br.readLine()+" ";
for(int i=0;i<str1.length();i++){
char x=str1.charAt(i);
if(x==' '){
str2=str3+" "+str2;
str3="";
}
else{
str3=str3+x; //HERE IS THE QUESTION PART(STRING+CHAR)
}
}
System.out.println(str2);
}
}
回答by ADTC
A char
can be implicitly converted into a String
. If Java didn't do this, you would have had to write it as following:
Achar
可以隐式转换为 a String
。如果 Java 没有这样做,您将不得不按如下方式编写它:
str3 = str3 + String.valueOf(x);
Heck, even the +
operator is overloaded for String
s in Java. Without which, you would have had to write it like this:
哎呀,甚至+
运算符String
在 Java 中也为s重载了。没有它,你将不得不这样写:
str3 = str3.concat(String.valueOf(x));
So implicit type conversions make it easy for you to write code. And it's easy for you to read and understand the same code:
因此,隐式类型转换使您可以轻松编写代码。您很容易阅读和理解相同的代码:
str3 = str3 + x;
//understanding: the string 'str3' is being joined with the character 'x'
Note, however that not everything can be implicitly converted. For example, this wouldn't work:
但是请注意,并非所有内容都可以隐式转换。例如,这行不通:
char y = str3 + x; //ERROR: cannot cast string to char!
Implicit conversion only works when a basic type can be converted to a more advanced type without data loss. Here, a String
can be thought of as a string of characters (think of those letter-bead bracelets). So a char
can be thought of as a string with just one character. So what the +
operator does is to add this character to the end of the other string full of characters.
隐式转换仅在基本类型可以转换为更高级的类型而不会丢失数据时才有效。在这里, aString
可以被认为是一串字符(想想那些字母珠手镯)。所以 achar
可以被认为是一个只有一个字符的字符串。那么+
运算符所做的就是将这个字符添加到另一个充满字符的字符串的末尾。
回答by nj-ath
When there is a conflict between the conversion between data types, the compiler always chooses the larger data type(in your case it is string). This is called implicit type conversion.
Say you have 2 cans 1 of 1ltr capacity and the other 2ltr. It is always safe to pour the contents of 1ltr can to the 2 ltr can.
当数据类型之间的转换发生冲突时,编译器总是选择较大的数据类型(在您的情况下它是字符串)。这称为隐式类型转换。
假设您有 2 罐 1 升容量和另一个 2 升容量。将 1 升罐中的物品倒入 2 升罐中始终是安全的。