java - Java 中 i +"abc"的类型

标签 java string stringbuilder

我正在调用

sb.append(i+"\n");

哪里

StringBuilder sb= new StringBuilder();
int i. 

i+"\n" 转换到这里的是什么 - StringStringBufferCharSequence ,字符数组,...?

StringBuilderappend(),每个参数都有一个 Object、一个 String、一个 < strong>StringBuilder(这是一个私有(private)方法),一个StringBuffer,一个CharSequence参数,字符数组,等等。我试图找出其中调用了哪些——希望避免调用 String 参数以进行快速处理。

提前致谢。

最佳答案

每当将运算符+应用于String和任何其他类型的变量时,就会产生String串联。 i + "\n" 的值是一个 String

java language specification涵盖了这个

If only one operand expression is of type String, then string conversion (§5.1.11) is performed on the other operand to produce a string at run time. The result of string concatenation is a reference to a String object that is the concatenation of the two operand strings. The characters of the left-hand operand precede the characters of the right-hand operand in the newly created string.

因此,被调用的 append() 方法是接受 String argument 的方法。 .

正如 @scottb 所评论的,Java 首先将 String 连接中涉及的对象转换为 String 对象。 This is described in the JLS chapter on String conversion.基元被拆箱为其包装类型。如果引用不是 null,则对引用的对象调用 toString() 方法。如果引用为 null,则使用 String "null"

关于java - Java 中 i +"abc"的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19919512/

相关文章:

java - 通过 BLE 将字节数组发送到 MCU

java - 在java中替换字符串中的数字

java - 使用 "+"运算符的非字符串操作数后,结果字符串会进入字符串池吗?

c# - 从属性返回字符串连接的最有效方法

c# - 在 C# 中,检查 stringbuilder 是否包含子字符串的最佳方法

java - Gradle java Web 应用程序无法在 heroku 上启动

java - JSP 作为电子邮件模板

javascript - 将数组形式的字符串转换为实际数组

c 读取字符串导致崩溃

python - 将字典中没有的单词替换为