【问题标题】:Possible lossy conversion from int to byte error in byte methods字节方法中从 int 到字节的可能有损转换错误
【发布时间】:2014-07-08 09:37:46
【问题描述】:

我有一个字节变量

byte A;

和 setters-getters 方法

    public void setA(byte A) throws NumberBelowEqualZeroException{
        if(A > 0)
            this.hoursPerWeek = hoursPerWeek;
        throw new NumberBelowEqualZeroException();
    }

    public byte getWorkingHoursPerWeek(){
        return hoursPerWeek;
    }

当我尝试在 main 中设置变量时

AClass.setA(0);

NetBeans 抱怨消息不兼容的类型。从 int 到 byte 的可能有损转换。以上都不是int。我无法理解发生了什么。我应该转换像byte A = new Byte(A) 这样的字节类型吗?感谢您的帮助。

【问题讨论】:

    标签: java byte type-conversion


    【解决方案1】:

    现在试试

    AClass.setA((byte)0)
    

    它抱怨是因为常量 0 是一个 int

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-12
      相关资源
      最近更新 更多