2015-08-04
[Android] 字串比較的兩種方式的比較 Comparison of Two ways of String Comparison: TextUtils.equals() and String.equals()
字串比較的兩種方式的比較
Comparison of Two ways of String Comparison:
TextUtils.equals() and String.equals()
[Java] String.valueOf() 和 Integer.toString(), Long.toString, Double.toString的差異. 為何String.valueOf()比較好?Why String.valueOf() is better than toString()?
為何String.valueOf()比 Integer.toString(), Long.toString, Double.toString好?
Why String.valueOf() is better than toString()?
1. If a value is null.
toString() would throw NullPointerException.
String.valueOf() would return a String "null".
當輸入值為null, String.valueOf()不會發生Exception.
2.
像這樣的一個function
public String example1(int num, boolean boo) {
return Integer.toString(num) + Boolean.toString(boo);
}
如果參數的type改變了, 那麼implemention就也要跟著改變
If the type of num and boo is changed, the the implementation have to change depends on the type of num and boo.
但如果使用String.valueOf() 那麼就不用改變implementation了
像下面這個function, 就算參數的type一直改, function implementation也都不用改
You don't have to change implementation when the type of num and boo is changed.
public String example2(int num, boolean boo) {
return String.valueOf(num) + " " + String.valueOf(boo);
}
3.
訂閱:
文章 (Atom)