append()メソッドのピット
public static void main(String[] args) {
String ab = null;
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(" ");
if(null== ab) {
stringBuffer.append(ab);
}
System.out.println(ab);
System.out.println(stringBuffer.toString());
}
:
null
null
null文字列が1つ増えました.
下面是撸源码时间←_←
java.lang.StringBuffer#append(java.lang.String) public synchronized StringBuffer append(String str) {
super.append(str); //
return this;
}
java.lang.AbstractStringBuilder#append(java.lang.String) /**
* Appends the specified string to this character sequence.
*
* The characters of the {@code String} argument are appended, in
* order, increasing the length of this sequence by the length of the
* argument. : If {@code str} is {@code null}, then the four
* characters {@code "null"} are appended.
* str null, “null” append 。
*
* Let n be the length of this character sequence just prior to
* execution of the {@code append} method. Then the character at
* index k in the new character sequence is equal to the character
* at index k in the old character sequence, if k is less
* than n; otherwise, it is equal to the character at index
* k-n in the argument {@code str}.
*
* @param str a string.
* @return a reference to this object.
*/
public AbstractStringBuilder append(String str) {
if (str == null) str = "null"; //
int len = str.length();
ensureCapacityInternal(count + len);
str.getChars(0, len, value, count);
count += len;
return this;
}
上のソースの注釈の言うのははっきりしていて、jdkは多くこの一挙の変換に[顔を覆います]!!!
したがって、フロントエンドに返されるデータを組み立てるときにappend()メソッドを使用すると、下の栗のようにappend()メソッドを使用する場合は、空を判断し、神馬が発生することに注意してください...私の神马はすべて知らないで、私の神马はすべて[颜を覆います]!!!append(coupon.getName());
:
String couponName = coupon.getName();
if(StringUtils.isNotBlank(couponName )){
append(couponName );
}
public synchronized StringBuffer append(String str) {
super.append(str); //
return this;
}
/**
* Appends the specified string to this character sequence.
*
* The characters of the {@code String} argument are appended, in
* order, increasing the length of this sequence by the length of the
* argument. : If {@code str} is {@code null}, then the four
* characters {@code "null"} are appended.
* str null, “null” append 。
*
* Let n be the length of this character sequence just prior to
* execution of the {@code append} method. Then the character at
* index k in the new character sequence is equal to the character
* at index k in the old character sequence, if k is less
* than n; otherwise, it is equal to the character at index
* k-n in the argument {@code str}.
*
* @param str a string.
* @return a reference to this object.
*/
public AbstractStringBuilder append(String str) {
if (str == null) str = "null"; //
int len = str.length();
ensureCapacityInternal(count + len);
str.getChars(0, len, value, count);
count += len;
return this;
}
append(coupon.getName());
:
String couponName = coupon.getName();
if(StringUtils.isNotBlank(couponName )){
append(couponName );
}