Topic: [讨论]两个基础问题

  Print this page

1.[讨论]两个基础问题 Copy to clipboard
Posted by: href
Posted on: 2004-09-03 10:18

不知道大家写代码的时候会不会注意
1。在比较null时,到底应该
x == null
or null == x
呢?两者据说是有区别的。

2。在int转String时,又用哪种方式最高效又简洁呢?
2.1 String s = i + "";
2.2 String s = Integer.toString(i);
2.3 String s = String.valueOf(i);
后两者好像是等价的

没事闲着讨论讨论

2.Re:[讨论]两个基础问题 [Re: href] Copy to clipboard
Posted by: why
Posted on: 2004-09-03 10:41

I read that String.valueOf(int) calls Integer.toString(int),
and s = i + "" further involves creation of StringBuffer

If you're serious about this, you may either
1. do some benchmarking stuff
2. take a look at Sun's Java source
3. compare compiled Java Assembly Language of themSmile

3.Re:[讨论]两个基础问题 [Re: href] Copy to clipboard
Posted by: href
Posted on: 2004-09-03 11:05

从source看不出 2.1和2.2,2.3的区别来。
老大,第三点用肉眼难度太大了吧

4.Re:[讨论]两个基础问题 [Re: href] Copy to clipboard
Posted by: why
Posted on: 2004-09-03 11:16

href wrote:
从source看不出 2.1和2.2,2.3的区别来。
老大,第三点用肉眼难度太大了吧

应该看得出 2.2 和 2.3 之间的分别
第三点,看不见笑脸吗?再来一个Wink


from String.java in src.zip of J2SDK
public static String valueOf(Object obj) {
   return (obj == null) ? "null" : obj.toString();
}

from Integer.java
public String toString() {
return String.valueOf(value);
}
What are you talking about?


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923