爲何byte取值負128到127.md

java設計byte類型爲1個字節,1個字節佔8位,即8bit,這是常識。java

另外,計算機系統中是用補碼來存儲的,首位爲0表示正數,首位爲1表示負數,因此有如下結論:面試

最大的補碼用二進制表示爲:01111111= 127後端

最小的補碼用二進制表示爲:10000000= -128多線程

關於補碼、原碼、反碼的計算原理能夠百度。架構

Byte的源碼:工具

/**

 * A constant holding the minimum value a {@code byte} can線程

 * have, -27.設計

 */code

public static final byte   MIN_VALUE = -128;教程

/**

 * A constant holding the maximum value a {@code byte} can

 * have, 27-1.

 */

public static final byte   MAX_VALUE = 127;

7是最高位,總共8bit,能夠看出byte佔1個字節,即8/8=1。

Integer源碼:

/**

 * A constant holding the minimum value an {@code int} can

 * have, -231.

 */

public static final int   MIN_VALUE = 0x80000000;

/**

 * A constant holding the maximum value an {@code int} can

 * have, 231-1.

 */

public static final int   MAX_VALUE = 0x7fffffff;

31是最高位,總共32bit,能夠看出int佔4個字節,即32/8=4。

其餘Short、Long的設計原理也同樣。

推薦去個人博客閱讀更多:

1.Java JVM、集合、多線程、新特性系列教程

2.Spring MVC、Spring Boot、Spring Cloud 系列教程

3.Maven、Git、Eclipse、Intellij IDEA 系列工具教程

4.Java、後端、架構、阿里巴巴等大廠最新面試題

生活很美好,明天見~

相關文章
相關標籤/搜索