英文:
==operator comparison of int and byte
问题
//code start
int a=3;
byte b=3;
if(a==b)  --> 返回 true
//code ends
- 
Query :
返回 true 是因为比较了 a 和 b 中的位。
a = 00000011
b = 00000011这里只进行位模式比较,int 中末尾的零不影响结果。
上述内容传达了什么?
 
英文:
//code start
int a=3;
byte b=3;
if(a==b)  --> returns true
//code ends
- 
Query :
IT returns true because compares the bits in a and b.
a = 00000011
b = 00000011here only bit pattern comparison is done and zeros left at the end in int doesn't matter.
what does the above line conveys??
 
答案1
得分: 1
在Java中,自动类型转换将任何较小的数据类型转换为两种类型中较大的类型。因此,当您将byte b与int a进行比较时,byte b会被转换为int b。
请注意,double是最大的数据类型,而byte是最小的。
英文:
In java automatic type conversion converts any small data type to the larger of the two types. So byte b is converted to int b when you are comparing it with int a.
Do know that double is the largest data type while byte is the smallest.
答案2
得分: 1
与您猜测的仅比较两个操作数的最低有效的8位不同,编译器会添加24个0或(如果字节为负数则为1)到字节中,使其与int相同大小。这称为_二进制数值提升_,在JLS §5.6.2中有详细规定。从byte到int的转换在JLS §5.1.2中有详细规定。
英文:
Rather than only comparing the least significant 8 bits of the two operands like you guessed, the compiler adds 24 0s or (or 1s if the byte is negative) to the byte to make it the same size as the int. This is known as binary numeric promotion and is specified in §5.6.2 of the JLS. The conversion from byte to int is specified in §5.1.2.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论