为什么Java HashMap 1<< 30的最大容量而不是1<< 31> [英] Why is the maximum capacity of a Java HashMap 1<<30 and not 1<<31?
问题描述
为什么Java HashMap 1 <30的最大容量并不是1 <31,即使int的最大值是2 31 31 <-1>?最大容量被初始化为 static final int MAXIMUM_CAPACITY = 1 << 30;
Java使用带符号整数,这意味着第一位用于存储符号数字(正/负)。
四字节整数有32位,其中数字部分可能由于签名位而只能跨越31位。这将数字的范围限制为 2 ^ 31 - 1 (归因于0)到 - (2 ^ 31)。
Why is the maximum capacity of a Java HashMap 1<<30 and not 1<<31, even though the max value of an int is 231-1? The maximum capacity is initialized as static final int MAXIMUM_CAPACITY = 1 << 30;
Java uses signed integers which means the first bit is used to store the sign of the number (positive/negative).
A four byte integer has 32 bits in which the numerical portion may only span 31 bits due to the signing bit. This limits the range of the number to 2^31 - 1 (due to inclusion of 0) to - (2^31).
这篇关于为什么Java HashMap 1<< 30的最大容量而不是1<< 31>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!