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类型的使用场合:
 由于不同的机器对于多字节数据(int 就是采用4个byte保存数据)的存储方式不同,可能是 低字节向高字节存储,也可能是从高字节向低字节存储,这样,在 分析网络协议或文件格时,为了解决不同机器上的字节存储顺序问题,用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  
这是补码的形式(10000001),取反加1可以得到源码(11111111)