Home  >  Q&A  >  body text

java中整数数值默认是int类型,这句话正确吗?

比如byte b = 1; char c = 1;,如果整数类型默认是int类型的话,那么byte b = 1;应该需要强转换才对,因为int是高精度的,但这里明显不报错,这是为什们呢?

高洛峰高洛峰2743 days ago1035

reply all(5)I'll reply

  • 迷茫

    迷茫2017-04-18 10:53:57

    First of all, the integer type defaults to int. There is no doubt about this.
    Secondly, within the allowed type range, here byte: -128~127
    And the integer int 1 is in this range, the compiler will automatically perform downward conversion of the constant.
    If the constant exceeds this range, the compiler will directly report an error.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 10:53:57

    Byte, short, char, and boolean are all processed as int at the bottom layer

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 10:53:57

    Wrong, two integers are added together. If it is less than or equal to int, the default is int. If one of them is long, the default is long;

    You try to declare two byte variables, and then add them and assign them to another byte variable. The compiler will prompt an error and forced type conversion is required

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:53:57

    Check this article: http://www.programgo.com/arti...

    reply
    0
  • 阿神

    阿神2017-04-18 10:53:57

    Divided into automatic conversion and forced conversion

    reply
    0
  • Cancelreply