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

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
T
Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
Intezer
C
Cyber Attacks, Cyber Crime and Cyber Security
The Register - Security
The Register - Security
量子位
Security Latest
Security Latest
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
MyScale Blog
MyScale Blog
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
Spread Privacy
Spread Privacy
Jina AI
Jina AI
博客园 - 【当耐特】
P
Palo Alto Networks Blog
Last Week in AI
Last Week in AI
SecWiki News
SecWiki News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
G
GRAHAM CLULEY
宝玉的分享
宝玉的分享
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
The Blog of Author Tim Ferriss
V
Vulnerabilities – Threatpost
有赞技术团队
有赞技术团队
T
Tor Project blog
H
Hacker News: Front Page
A
Arctic Wolf
NISL@THU
NISL@THU
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
V
V2EX
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
Know Your Adversary
Know Your Adversary
P
Privacy International News Feed
I
InfoQ
D
Docker
L
LINUX DO - 最新话题
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
U
Unit 42

博客园 - 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转换的情况。