【发布时间】:2017-04-26 05:18:52
【问题描述】:
我想将我的布尔对象转换为字符串。以下哪一项是更好/正确的做法,为什么?
boolean booleanValue = true;
String strValue = Boolean.toString(booleanValue);
或
boolean booleanValue = true;
String strValue = booleanValue + "";
编辑
下面的答案提示我添加更多
boolean booleanValue = true;
String strValue = String.valueOf(booleanValue);
对于像Integer这样的非原始数据类型
Integer i = 10;
String strValue = i.toString();
所以,最后我们有了三种不同的类型 Boolean.toString() ,与 + 、 String.valueOf() 连接以将任何数据类型转换为 String
【问题讨论】:
-
@ΦXocę웃Пepeúpaツ:编辑过一样的..请检查。
-
它们是等价的。后者调用前者。
标签: java type-conversion