# 基本数据类型

## java中含有8种数据结构和一种引用结构

| 数据类型               | 内存大小(单位:w位)          | 取值                        |
| ------------------ | -------------------- | ------------------------- |
| byte(数值型)          | 8                    | -128\~127                 |
| short(数值型)         | 16                   | -32768\~32767             |
| int(数值型）           | 32                   | -2147483648\~2147483647   |
| long(数值型)          | 64                   | -2^63\~2^63-1(还有一个0，所以-1) |
| float(浮点型）         | 32                   | IEEE 754                  |
| double(浮点型)        | 64                   | IEEE 754(浮点标记法)           |
| char(字符型可以与byte转化) | 16                   | 对应数值的unicode              |
| boolean(布尔类型)      | 具体看虚拟机（一般为8）         | true \| false             |
| 引用                 | 32(32位机子)\|64(64位机子) | 对象引用大小                    |

## java对象结构

| 类型                                                    | 对象     | 数组     |
| ----------------------------------------------------- | ------ | ------ |
| 对象头（markword）32位(32位机子)64(64位机子)                      | 有      | 有（会压缩） |
| 对象头（kclass,类型指针）32位(32位机子\|最大堆内存小于32g\|开启压缩)64(64位机子) | 有(会压缩) | 有(会压缩) |
| 对象头(int)                                              | 无      | 有      |
| data                                                  | 有      | 有      |
| padding(按8对齐)                                         | 有      | 有      |

注意：开启-XX:+UseCompressedOops后或者最大堆内存下于32g,那么64位下kclass指针会被压缩位4byte

计算公式

* 在32位系统下，存放Class指针的空间大小是4字节，MarkWord是4字节，对象头为8字节。
* 在64位系统下，存放Class指针的空间大小是8字节，MarkWord是8字节，对象头为16字节。
* 64位开启指针压缩的情况下，存放Class指针的空间大小是4字节，MarkWord是8字节，对象头为12字节。
* 数组长度4字节+数组对象头8字节(对象引用4字节（未开启指针压缩的64位为8字节）+数组markword为4字节（64位未开启指针压缩的为8字节）)+对齐4=16字节。
* 静态属性不算在对象大小内。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://298073941.gitbook.io/memo/2020-05/java-ji-chu-1/ji-ben-shu-ju-lei-xing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
