知道String实现了CharSequence接口,为什么StringBuilder有一个CharSequence构造函数和另一个String构造函数? javadoc中没有指示!
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {...}
public final class StringBuilder
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
{
...
/**
* Constructs a string builder initialized to the contents of the
* specified string. The initial capacity of the string builder is
* {@code 16} plus the length of the string argument.
*
* @param str the initial contents of the buffer.
*/
public StringBuilder(String str) {
super(str.length() + 16);
append(str);
}
/**
* Constructs a string builder that contains the same characters
* as the specified {@code CharSequence}. The initial capacity of
* the string builder is {@code 16} plus the length of the
* {@code CharSequence} argument.
*
* @param seq the sequence to copy.
*/
public StringBuilder(CharSequence seq) {
this(seq.length() + 16);
append(seq);
}
...
}
答案 0 :(得分:13)
优化。如果我没记错的话,有两种实现append的方法。 k1 k2 k3 k4
0 1 A X NaN
1 2 B X NaN
2 3 A Y NaN
3 4 D NaN M
比SELECT * INTO #TempTable FROM TableName
EXEC [tempdb].[dbo].[sp_help] N'#TempTable'
是字符串的append(String)
更有效。如果我必须执行一些额外的例程来检查以确保append(CharSequence)
与String兼容,请将其转换为String,然后运行append(String),那比直接执行append(String)要长。结果相同。不同的速度。