Is there a default type for numbers in Java

前端 未结 2 2055
南旧
南旧 2020-11-28 15:39

If I write something like this

System.out.println(18);

Which type has the \'18\'? Is it int or byte? Or doesn\'t it have

2条回答
  •  情话喂你
    2020-11-28 16:13

    The JLS-4.2.1 - Integral Types and Values

    The values of the integral types are integers in the following ranges:

    • For byte, from -128 to 127, inclusive
    • For short, from -32768 to 32767, inclusive
    • For int, from -2147483648 to 2147483647, inclusive
    • For long, from -9223372036854775808 to 9223372036854775807, inclusive
    • For char, from '\u0000' to '\uffff' inclusive, that is, from 0 to 65535

    And JLS-3.10.1 - Integer Literals

    An integer literal is of type long if it is suffixed with an ASCII letter L or l (ell); otherwise it is of type int (§4.2.1).

    Finally, JLS-3.10.2 - Floating-Point Literals includes

    A floating-point literal is of type float if it is suffixed with an ASCII letter F or f; otherwise its type is double and it can optionally be suffixed with an ASCII letter D or d (§4.2.3).

    As for byte b = 3; it is a Narrowing Conversion from int to byte.

提交回复
热议问题