【问题标题】:When do I have to cast to byte when initialize a byte array in java?在 java 中初始化字节数组时,我什么时候必须转换为字节?
【发布时间】:2015-06-13 12:37:01
【问题描述】:

代码如下:

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();
        }

    }
}

有两个字节数组初始化(标记为///////),第一个我没有转换,第二个我必须,否则我得到这个错误:

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

为什么?

【问题讨论】:

    标签: java arrays int type-conversion byte


    【解决方案1】:

    0xff 不适合byte 类型的可表示数字范围。适合byte 的最大值是127,其中0xff 代表255。因此,在这种情况下你需要一个演员表。

    如果数字是可表示的,例如在您分配 0x6c 的第一种情况下,Java 编译器会在应用从 intbyte 的缩小转换后接受它而不进行强制转换。

    integer 值缩小到byte 时,除目标类型可表示的最低位之外的所有位都将被丢弃。这意味着对于整数0xff(即00000000000000000000000011111111),采用最低8 位,从而得到字节11111111,即-1。因此,转换改变了值初始数字的符号。

    【讨论】:

      【解决方案2】:

      那是因为 0x01 是一个十六进制数字,已经适合一个字节,但 0xFF 不适合,所以编译器告诉你你可能会丢失信息。所以你需要明确地将它从 int 转换为 byte。

      【讨论】:

        【解决方案3】:

        byte 类型的范围是从-128 to 127。所以像0x6c(十进制为108)这样的数字是范围。但是0xff(即255)超出了这个范围,需要强制转换才能得到对应的字节值:-1

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-10-21
          • 2012-02-12
          • 1970-01-01
          • 2012-06-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多