Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do I have to cast to byte when initialize a byte array in java?

Here is the code:

import java.io.*;

public class Bed {
    private String bedfn;
    private int bytes_snp;
    private int nindiv;
    private long bedsize;
    private int nsnp;
    private byte[] magicBytes;

    public Bed(String bedfn, int bytes_snp, int nindiv) {
        this.bedfn = bedfn;
        this.bytes_snp = bytes_snp;
        this.nindiv = nindiv;
        bedsize = (new File(bedfn)).length();
        nsnp = (int) ((bedsize - 3) / bytes_snp);
        ///////////////////////////////////////
        magicBytes = new byte[] {0x6c, 0x1b, 0x01};
    }

    //...

    public OutputStream writeBytes(OutputStream fileStream, byte[] bytes) {
        try{
            fileStream.write(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileStream;
    }

    public OutputStream writeBytes(OutputStream fileStream) {
        return writeBytes(fileStream, magicBytes);
    }

    public OutputStream writeBytes(String filename, byte[] bytes) throws FileNotFoundException {
        FileOutputStream fileOutputStream = new FileOutputStream(filename);
        BufferedOutputStream fileBufferedOutput = new BufferedOutputStream(fileOutputStream);
        return writeBytes(fileBufferedOutput, bytes);
    }


    public OutputStream writeBytes(String filename) throws FileNotFoundException {
        return writeBytes(filename, magicBytes);
    }




    public static void main(String[] args) throws IOException {
        String filename = "/Users/kaiyin/personal_config_bin_files/workspace/rbed2/inst/extdata/test.bed";
        Bed bedobj = new Bed(filename, 2, 6);

        OutputStream outputStream = bedobj.writeBytes("/tmp/x.bed");
        try{
            ///////////////////////////////////////
            outputStream.write(new byte[] {(byte) 0xff, (byte) 0xff});
        } finally {
            outputStream.close();
        }

    }
}

There are two byte array initializations (marked with ///////), in the first I didn't cast, in the second I have to, otherwise I get this error:

Error:(194, 45) java: incompatible types: possible lossy conversion from int to byte

Why?

like image 270
qed Avatar asked Jan 08 '23 04:01

qed


1 Answers

0xff does not fit in the range of representable numbers in the byte type. The maximum value that fits in a byte is 127 where as 0xff represents 255. Therefore you need a cast in this case.

When the number is representable, for instance in the first case where you assign 0x6c the Java compiler accepts it without a cast after applying a narrowing conversion from int to byte.

When the integer value is narrowed to a byte, all bits except the lowest representable by the target type are discarded. This means that for the integer 0xff which is 00000000000000000000000011111111, the lowest 8 bits are taken, resuling in the byte 11111111 which is -1. Therefore, the conversion changed the value and the sign of the initial number.

like image 59
M A Avatar answered Jan 11 '23 22:01

M A