如何String对象转换为Boolean对象? [英] How to convert String object to Boolean Object?

查看:111
本文介绍了如何String对象转换为Boolean对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何字符串对象转换为布尔对象?

推荐答案

尝试(取决于你想要什么样的结果类型):

Try (depending on what result type you want):

Boolean boolean1 = Boolean.valueOf("true");
boolean boolean2 = Boolean.parseBoolean("true");

优点:


  • 布尔:这并不布尔创建新实例,所以性能更好(和更少的垃圾收集)。它重用任 Boolean.TRUE Boolean.FALSE 的两个实例。

  • 布尔:无需例如,你可以使用原始类型

  • Boolean: this does not create new instances of Boolean, so performance is better (and less garbage-collection). It reuses the two instances of either Boolean.TRUE or Boolean.FALSE.
  • boolean: no instance is needed, you use the primitive type.

有关的正式文件是在的Javadoc

更新:

自动装箱也可以使用,但它有一个性能成本。结果
我建议使用它,只有当你将不得不投自己,而不是当演员是可以避免的。

Autoboxing could also be used, but it has a performance cost.
I suggest to use it only when you would have to cast yourself, not when the cast is avoidable.

这篇关于如何String对象转换为Boolean对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆