简体   繁体   English

类型转换:byte vs int (Java)

[英]Casting of types: byte vs int (Java)

    byte b = 1;
    b = -b; //i get highlited in red in here
    
    int a = 1;
    a = -a; //it is ok for jvm

why if (b = -b), then I get a mistake, but if i do the same with int, it is OK?为什么如果(b = -b),那么我会出错,但是如果我对 int 做同样的事情,就可以了? What is the reason?是什么原因?

Sorry for such a newbie question, but I can't understand reason myself, neither found answer in google of SO.对于这样一个新手问题,我很抱歉,但我自己也无法理解原因,也没有在谷歌的 SO 中找到答案。

There is a good article " The mystery of the negative byte value in Java ".有一篇好文章《 Java中负字节值之谜》。

Also,还,

Red highlighted tell you:红色高亮告诉你:

Required type: byte所需类型: byte

Provided: int提供: int

在此处输入图像描述

So, unary minus operator cast byte to int .因此,一元减号运算符将byte转换为int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM