惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
博客园_首页
W
WeLiveSecurity
S
Secure Thoughts
S
Security @ Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hugging Face - Blog
Hugging Face - Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Hacker News: Front Page
Project Zero
Project Zero
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
U
Unit 42
N
News and Events Feed by Topic
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
Forbes - Security
Forbes - Security
T
Tor Project blog
I
Intezer
B
Blog
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
Schneier on Security
Schneier on Security
T
Threat Research - Cisco Blogs
AI
AI
Google DeepMind News
Google DeepMind News
L
LINUX DO - 最新话题
Cloudbric
Cloudbric
L
Lohrmann on Cybersecurity
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
P
Privacy International News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
PCI Perspectives
PCI Perspectives
Y
Y Combinator Blog
Spread Privacy
Spread Privacy
Simon Willison's Weblog
Simon Willison's Weblog
罗磊的独立博客
Vercel News
Vercel News
A
Arctic Wolf
The Register - Security
The Register - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
H
Heimdal Security Blog
Know Your Adversary
Know Your Adversary
P
Proofpoint News Feed
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed

博客园 - AllenChen

x86,amd64,x86-64,x64区别 SQO2008R2配置管理工具服务显示远程过程调用失败 ---转载 用户 登录失败。原因: 该帐户的密码必须更改 sql2008 -------源于网络 ReSharper 配置及用法(转) 好久没写博客了 win7语音回声 firefox几个常用快捷键 “懒 ”和“训练不得法 ” 技术 选对了路吗 转载:保存一份牛人的简历 转 手动卸载Firefox Synchronisation Extension及其他顽固扩展 (转)富人狼性 穷人羊性(商人必读) 也许,老天是故意的. 不过有提醒的 曾经为怎么读而迷茫--或者说,为为什么读而迷茫 一切都是浮云,只剩白马 整了几个小时,终于自由 火狐4 IE9 和十年前一样的胆战心惊
如何将int转换成String
AllenChen · 2011-08-27 · via 博客园 - AllenChen

转自:

http://hi.baidu.com/shadouyou/blog/item/ccbe8d2f1258273b1e308971.html/cmtid/bf2625d12ae630379a5027fd

如何将int转换成String 在java中

2007年09月10日 星期一 下午 05:06

方法1
int i=10;
String s=""+i;
这是利用java的toString机制来做的转换,任何类型在和String相加的时候,都会先转换成String。

方法2
int i=10;
String s=String.valueOf(i);
这是利用String类提供的工厂方法来做的转换。

1.) String s = String.valueOf(i);

2.) String s = Integer.toString(i); 

3.) String s = "" + i; 


哪种方法好?
第一种?比较方便。
第二种?比较高效。

下面是一段测试程序。

Random ra=new Random(new java.util.Date().getTime());
String tmp=null;
int runtimes=1000000;
int range=50;

for (int i = 0; i <runtimes; i++) {
tmp=String.valueOf(ra.nextInt(range));
}

for (int i = 0; i <runtimes; i++) {
tmp = ""+ra.nextInt(range);
}

其结果,第一个循环只用了0.841秒,而第二个,则用了2.624秒。

这是因为String类是一个不可变对象,这就使得String类可以随意的重用,而不会有问题。
事实上在系统内部是有一个String对象的缓冲池,当使用String.valueOf方法的时候,会尽
可能的从这个池中取出符合条件的对象。

所以,请尽可能的使用第二种方法来做转换,同样的情况也适用于float, double, byte等
类型向String转换的情况。