java int size fixed or variable?

后端 未结 5 2122
攒了一身酷
攒了一身酷 2021-02-12 19:38

Is integer size in java of fixed length or variable size?

ex: 1 or 10000 does both the number takes same space during allocation?

相关标签:
5条回答
  • 2021-02-12 19:47

    Java integers are 32 bits (4 octets) as per the JLS.

    The integral types are byte, short, int, and long, whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, respectively.

    Source: JLS §4.2 Primitive Types and Values

    0 讨论(0)
  • 2021-02-12 19:47

    It depends on the JVM implementation.

    In typical implementations, boolean, byte, char, short and int will be 32-bits always on the stack while long will be 64-bits on the stack. In typical implementations bytes, chars, shorts, ints and longs will be their "native" size in arrays (that is, 8, 16, 16, 32 and 64-bit respectively).

    In typical implementations the size of integers within structures will be their native size if alignment allows it.

    It's certainly possible that some very unusual implementations could use variable length integers for structures, or even on the stack or in arrays (but that's even harder to imagine and more obscure) - but I certainly haven't seen any that do.

    0 讨论(0)
  • 2021-02-12 20:02

    It's fixed in size. All ints in Java are 32 bits, both from the programmer's perspective and the machine's.

    The Java VM specification, which describes the JVM bytecode format, mentions that each int is 32 bits. (Aside: boolean values can take up any number of bits, as can objects.)

    0 讨论(0)
  • 2021-02-12 20:10

    Here's the datasizes for the Java primitive types.

    Interesting to note here is that the size of boolean is not clearly defined, but it's usually 8 bits.

    0 讨论(0)
  • 2021-02-12 20:10

    An int or an Integer has fixed size. A BigInteger has variable size.

    0 讨论(0)
提交回复
热议问题