为什么Java HashMap 1<< 30的最大容量而不是1<< 31> [英] Why is the maximum capacity of a Java HashMap 1<<30 and not 1<<31?

查看:649
本文介绍了为什么Java HashMap 1<< 30的最大容量而不是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&lt;&lt; 30的最大容量而不是1&lt;&lt; 31&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆