我正在调用
sb.append(i+"\n");
哪里
StringBuilder sb= new StringBuilder();
int i.
i+"\n" 转换到这里的是什么 - String、StringBuffer、CharSequence ,字符数组,...?
StringBuilder 有 append(),每个参数都有一个 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/