Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
463 views
in Technique[技术] by (71.8m points)

java - Java - 将整数转换为字符串[duplicate](Java - Convert integer to string [duplicate])

Given a number:

(给出一个数字:)

int number = 1234;

Which would be the "best" way to convert this to a string:

(这将是将其转换为字符串的“最佳”方式:)

String stringNumber = "1234";

I have tried searching (googling) for an answer but no many seemed "trustworthy".

(我曾尝试搜索(谷歌搜索)一个答案,但没有多少似乎“值得信赖”。)

  ask by Trufa translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are multiple ways:

(有多种方式:)

  • String.valueOf(number) (my preference)

    (String.valueOf(number) (我的偏好))

  • "" + number (I don't know how the compiler handles it, perhaps it is as efficient as the above)

    ("" + number (我不知道编译器如何处理它,也许它与上面一样有效))

  • Integer.toString(number)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...