When do I need to give a byte when initializing 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 as ///////), in the first one that I haven't spoken, in the second that I need, otherwise I get this error:

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

      

Why?

+3


source to share


3 answers


0xff

does not fit into the range of representable numbers in the type byte

. The maximum value that matches byte

is 127

where 0xff

represents 255

. Therefore, in this case, you need to make a throw.

When a number is present, for example in the first case when you assign 0x6c

, the Java compiler accepts it without being cast after applying a narrowing conversion from int

to byte

.



When the value is integer

narrowed to byte

, all but the smallest bits represented by the target type are discarded. This means that for an integer 0xff

that is 00000000000000000000000011111111

, the least significant 8 bits are accepted, returning to a byte 11111111

that is -1

. Therefore, the transformation changed the meaning and sign of the seed.

+5


source


This is because 0x01 is a hexadecimal number that already fits into the byte, but 0xFF does not, so the compiler tells you that you might lose information. So you need to explicitly pass it from int, to byte.



+2


source


The range of type is byte

from -128 to 127 . So a number like 0x6c

(which is 108 decimal) is a range. But 0xff

(which is 255) exceeds this range and must be executed to get the corresponding byte value: -1.

+1


source







All Articles