> For the complete documentation index, see [llms.txt](https://yangsx95.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yangsx95.gitbook.io/notes/programming-language/kotlin/shu-ju-lei-xing.md).

# 数据类型

Kotlin是一门强类型语言，在声明变量时，必须先声明其类型，并且变量的类型不可变，与值类型需匹配。同时，Kotlin也是一门静态类型语言，在编译期间就会对变量类型进行检测，如果不通过就会编译报错。

## 值类型与引用类型

Java 把基本数据类型和引用类型做了区分。一个基本数据类型（如`int`）的变量直接存储了它的值，而一个引用类型（如 `String`）的变量存储的是指向包含该对象的内存地址的引用。基本数据类型的值能够更高效地存储和传递，但是你不能对这些值调用方法，或是把他们存放在集合中。 Java 提供了特殊的包装类型（如`Integer`）在你需要对象的时候对基本数据类型进行封装。因此，你不能用 `Collection<int>` 来定义一个整数的集合，而必须用`Collection<Integer>`来定义。

与Java不同的是Kotlin并不区分基本类型和包装类型，在编码阶段，他只有一个类型`Int`，避免了Java中基本数据类型与包装类型的转换以及混乱的其他问题。

虽然 kotlin 是用同一个类型表示基本数据类型和引用类型，但是在运行时，数字类型会尽可能地使用最高效的方式来表示。大多数情况下，对于

* 变量
* 属性
* 参数
* 返回类型

kotlin 的 `Int` 类型会被编译成 Java 基本数据类型 `int` 。唯一例外是`泛型类`，会被编译成对应的 Java 包装类：

```kotlin
fun testBaseDataType(params : Int): Int {
    val i: Int = 2
    val list: List<Int> = listOf(1, 2, 3)
    return  i
}
```

上述代码编译为class的Java表现为:

```java
// 参数和返回类型变成 int 
public static final int testBaseDataType(int params) {    
   int i = 2;    // 变量变成 int 
    //集合变成 Integer
   List list = CollectionsKt.listOf(new Integer[]{1, 2, 3});    
   return i;
}
```

## 字面量

字面量是指直接表示数据值的语法表示法。Kotlin支持多种类型的字面量：

| 字面量表示                                   | 字面量会被推断的数据类型                          | 备注         |
| --------------------------------------- | ------------------------------------- | ---------- |
| `10`                                    | 如果不超过Int最大值，类型为Int；如果超过Int最大值，类型为Long | 十进制        |
| `10L`                                   | Long                                  | 十进制        |
| `0b101`                                 | Int，超为Long                            | 八进制        |
| `0x0F`                                  | Int，超为Long                            | 十六进制       |
| `0b00001011`                            | Int，超为Long                            | 二进制        |
| `1_000_000`                             | Int，超为Long                            | 下划线易读表示    |
| `999_99_9999L`                          | Int，超为Long                            | 下划线易读表示    |
| `0xFF_EC_DE_5E`                         | Int，超为Long                            | 下划线易读表示    |
| `0b11010010_01101001_10010100_10010010` | Int，超为Long                            | 下划线易读表示    |
| `1.23`                                  | Double                                |            |
| `1.23f`、`1.23F`                         | Float                                 |            |
| `1.23e-4`                               | Double                                | 科学计数法      |
| `Double/Float.NaN`                      | Double/Float                          | 表示非数字      |
| `Double/Float.*_INFINITY`               | Double/Float                          | 表示无穷大、无穷小  |
| `'a'`                                   | Char                                  |            |
| `"abc"`                                 | String                                |            |
| `true`                                  | Boolean                               |            |
| `false`                                 | Boolean                               |            |
| `null`                                  | 空                                     |            |
| `[1,2,3]`                               | in 数组                                 |            |
| `1...10`                                | in 范围                                 | 表示从1到10的范围 |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://yangsx95.gitbook.io/notes/programming-language/kotlin/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.
