问题描述
byte a = 1;
byte b = 1;
byte c = a + b;
引发错误:可能会失去精确度
Throws error: possible loss of precision
byte subt = a_s - a_b;
^
required: byte
found: int
这是行为与jvm或它在java语言中定义有关。
Is this behavior has something to do with jvm or its been defined in java language .
编辑:如果它是用java语言定义那么就做因为记住jvm?
EDIT : And if it is defined in java language then does it because of keeping jvm in mind ?
意味着如果java支持 byte
datatype那么为什么操作on byte
results int
Means if java supports byte
datatype then why operation on byte
results int
推荐答案
因为这就是Java虚拟机的设计方式。没有指令集对字节类型执行操作。相反, int
类型的指令集用于 boolean
, byte
, char
,短
类型。
Because that's how the Java Virtual Machine is designed. There is no instruction set to perform operation on a byte type. Rather the instruction set for int
type is used for the operation on boolean
, byte
, char
, and short
types.
来自:
背后的原因也在该部分中指明:
The reason behind this is also specified in that section:
有关所有内容的详细信息指令集可用于各种类型,您可以查看该部分中的表。
For the details on what all instruction sets are available for various types, you can go through the table in that section.
还有一个表指定实际类型到JVM计算类型的映射:
There is also a table specifying the mapping of actual type to the JVM computational type:
这篇关于由于java语言规则或jvm,是否将字节转换为int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!