带整数比较的字节不起作用
Byte with Integer comparsion not working
我有简单的代码片段
getMarkerCode() == -1 ? null : getMarker(getMarkerCode());
其中 getMarkerCode()
总是 return -1(字节类型)。但是每次都运行 getMarker(getMarkerCode())
方法。
即使修改条件为:
if(getMarkerCode() == (byte)-1)
return null;
else
return getMarker(getMarkerCode());
结果未更改。怎么了?
可以不换行到 Byte
class?
Byte.compare(byte, byte) 要求 SDK >= 19,但应用程序支持 API level 16
Referring to this post 您应该使用 compareTo
使用字节而不是注释中指出的 @Sergey
字节。你可以很容易地做到这一点,因为你正在实例化 "by hand" 更改类型不会有任何问题。
比字节应该像 new Byte("-1")
new Byte("-1").compareTo(getMarkerCode()) == 0 ? null : getMarker(getMarkerCode());
结果是:
Returns | Meaning
--------------------------------------------------------------------
0 | if this Byte is equal to the argument Byte;
less than 0 | if this Byte is numerically less than the argument Byte;
greater than 0 | if this Byte is numerically greater than the argument Byte (signed comparison).
备注
从字节到字节你可以简单地转换
byte b;
//plus instantiating
//...
Byte bObj = b;
如我所料,问题出在 Android studio 中的 Instant 运行 选项。我禁用它,问题就消失了。
小心 Instant 运行
我有简单的代码片段
getMarkerCode() == -1 ? null : getMarker(getMarkerCode());
其中 getMarkerCode()
总是 return -1(字节类型)。但是每次都运行 getMarker(getMarkerCode())
方法。
即使修改条件为:
if(getMarkerCode() == (byte)-1)
return null;
else
return getMarker(getMarkerCode());
结果未更改。怎么了?
可以不换行到 Byte
class?
Byte.compare(byte, byte) 要求 SDK >= 19,但应用程序支持 API level 16
Referring to this post 您应该使用 compareTo
使用字节而不是注释中指出的 @Sergey
字节。你可以很容易地做到这一点,因为你正在实例化 "by hand" 更改类型不会有任何问题。
比字节应该像 new Byte("-1")
new Byte("-1").compareTo(getMarkerCode()) == 0 ? null : getMarker(getMarkerCode());
结果是:
Returns | Meaning
--------------------------------------------------------------------
0 | if this Byte is equal to the argument Byte;
less than 0 | if this Byte is numerically less than the argument Byte;
greater than 0 | if this Byte is numerically greater than the argument Byte (signed comparison).
备注
从字节到字节你可以简单地转换
byte b;
//plus instantiating
//...
Byte bObj = b;
如我所料,问题出在 Android studio 中的 Instant 运行 选项。我禁用它,问题就消失了。
小心 Instant 运行