==操作符比较int和byte

huangapple go评论155阅读模式
英文:

==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 = 00000011

    here 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 bint a进行比较时,byte b会被转换为int b

请注意,double最大的数据类型,而byte最小的。

==操作符比较int和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.

==操作符比较int和byte

答案2

得分: 1

与您猜测的仅比较两个操作数的最低有效的8位不同,编译器会添加24个0或(如果字节为负数则为1)到字节中,使其与int相同大小。这称为_二进制数值提升_,在JLS §5.6.2中有详细规定。从byteint的转换在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.

huangapple
  • 本文由 发表于 2020年7月21日 18:59:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63013056.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定