/java:15:错误:不兼容的类型:从int到byte-byte-myTotalByte=(myMinByteValue/2)的可能有损转换;^1错误
错误:-/Main.java:15:错误:不兼容的类型:从int到byte的可能有损转换 字节myTotalByte=(myMinByteValue/2); ^/java:15:错误:不兼容的类型:从int到byte-byte-myTotalByte=(myMinByteValue/2)的可能有损转换;^1错误,java,Java,错误:-/Main.java:15:错误:不兼容的类型:从int到byte的可能有损转换 字节myTotalByte=(myMinByteValue/2); ^ 1错误有两种方法可以将字节类型变量设置为最终: class Main { public static void main(String[] args) { int myMinIntValue = Integer.MIN_VALUE; System.out.println("MY INteger
1错误有两种方法可以将
字节
类型变量设置为最终
:
class Main {
public static void main(String[] args) {
int myMinIntValue = Integer.MIN_VALUE;
System.out.println("MY INteger Value is =" + myMinIntValue);
byte myMinByteValue = Byte.MIN_VALUE;
System.out.println("MY byte Value is =" + myMinByteValue);
int myTotal = (myMinIntValue);
System.out.println(myTotal);
byte myTotalByte = (myMinByteValue / 2);
System.out.println(myTotalByte);
}
}
或您可以将其强制转换为字节
,并明确告知编译器
final byte myMinByteValue = Byte.MIN_VALUE;
要了解更多信息,请访问java 该2
类型为int
,您必须将其转换为:
byte myTotalByte = (byte) (myMinByteValue / 2);
那是iirc,因为我现在不在电脑前
byte myTotalByte = myMinByteValue / (byte)2;
2
是一个整数文本,因此结果也是一个整数。但是,即使您编写myMinByteValue/(byte)2
,它仍然是一个整数,因为int
是JVM中的最小类型大小(byte
在JVM内部被视为int
)。将它转换为byteIt,在我尝试除法(myminitvalue/2)时它已经起作用了…但不是(myMinByeValue/2)..<代码>字节myTotalByte=(字节)(myMinByteValue/2)代码>它也将不起作用,并将得到相同的错误。
byte myTotalByte = (byte)(myMinByteValue / 2);