问题描述
我找不到在布尔
类的任何方法序列化布尔1和0,而不是真与假。
I can't find any method on the Boolean
class to serialize a Boolean to "1" and "0" instead of "true" and "false".
有没有本地函数来做到这一点?如果没有,什么是最好的方式(最优化的方式)?
Is there any native function to do that ? If not, what is the best way (most optimized way) ?
更新:我真的意味着产生字符串
出的布尔
Update: I indeed mean to produce a String
out of a Boolean
.
推荐答案
的如果的你在谈论生产字符串
从给定布尔
,则没有,有产生0或1没有内置的方法,但是你可以很容易地把它写:
If you're talking about producing a String
from a given Boolean
, then no, there is no built-in method that produces "0" or "1", but you can easily write it:
public static String toNumeralString(final Boolean input) {
if (input == null) {
return "null";
} else {
return input.booleanValue() ? "1" : "0";
}
}
根据您的使用情况下,可能更适合让它抛出一个 NullPointerException异常
如果输入
为空。如果这是你的话,那么你可以在方法降低到第二单独回报
行。
Depending on your use case, it might be more appropriate to let it throw a NullPointerException
if input
is null. If that's the case for you, then you can reduce the method to the second return
line alone.
这篇关于序列化布尔值,QUOT&1 QUOT;和" 0 QUOT;代替"真"和"假"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!