byte与int的区别：

• byte uses 1 byte while int uses 4 bytes.
• integer literals like "45" are of int not byte.If you want a literal to be a byte, you have to cast it: "(byte)45".
• When values are promoted as part of an expression or as parameters to a method call, they may be promoted to int, but never to byte.
• Many parts of the Java language used int, but none of them use byte. For example, the length of an array is an int.

#### byte类型的使用场合：

/**
* Convert  an int to a byte array
*
* @param value int
* @return byte[]
*/
public static byte[] intToByteArray(int value) {
byte[] b = new byte[4];

// 使用4个byte表示int
for (int i = 0; i < 4; i++) {
int offset = (b.length - 1 - i) * 8;  // 偏移量
b[i] = (byte) ((value >> offset) & 0xFF); //每次取8bit
}
return b;
}

/**
* Convert the byte array to an int starting from the given offset.
*
* @param b The byte array
* @param offset The array offset,如果byte数组长度就是4，则该值为0
* @return The integer
*/

public static int byteArrayToInt(byte[] b, int offset) {
int value = 0;
for (int i = 0; i < b.length; i++) {
int shift = (b.length - 1 - i) * 8;
value += (b[i + offset] & 0xFF) << shift;
}
return value;
}

public static void main(String[] args) {
byte[] bb=intToByteArray(255);
for(int i=0;i<bb.length;i++){
System.out.println(bb[i]);
}
}

0
0
0
-1