2 u014297278 u014297278 于 2014.11.11 12:54 提问

请问byte怎么定义,以及比较问题

public static final byte WAIT_BYTE=00000000;
public static final byte WAIT_BALANCE_BYTE=(byte) 00010100;
public static final byte WAIT_RELAX_BYTE_=(byte) 00010100;
public static final byte WAIT_PERFORMANCE_BYTE_=(byte) 00010110;
public static final byte WAIT_MYSESSION1_BYTE_=(byte) 00011000;
public static final byte WAIT_MYSESSION2_BYTE_=(byte) 00011010;
我这样定义对吗

还有请问接收到的byte也是0101这样的吗?可以直接拿上面的直接==比较是否相等吗?
麻烦大家

2个回答

feiyun0112
feiyun0112   Rxr 2014.11.11 13:09

public static final byte WAIT_BYTE=0;
public static final byte WAIT_BALANCE_BYTE=20;

eagleyan
eagleyan   Rxr 2014.12.01 09:08

如果你在使用jdk7以上版本,可以这样写
public static final byte WAIT_BYTE=0b00000000;
public static final byte WAIT_BALANCE_BYTE=0b00010100;
public static final byte WAIT_RELAX_BYTE_=0b00010100;
public static final byte WAIT_PERFORMANCE_BYTE_=0b00010110;
public static final byte WAIT_MYSESSION1_BYTE_=0b00011000;
public static final byte WAIT_MYSESSION2_BYTE_= 0b00011010;
为了更好阅读,你甚至可以写成

public static final byte WAIT_BYTE=0b0000_0000;
public static final byte WAIT_BALANCE_BYTE=0b0001_0100;
public static final byte WAIT_RELAX_BYTE_=0b0001_0100;
public static final byte WAIT_PERFORMANCE_BYTE_=0b0001_0110;
public static final byte WAIT_MYSESSION1_BYTE_=0b0001_1000;
public static final byte WAIT_MYSESSION2_BYTE_= 0b0001_1010;
byte之间可以用==比较

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!