跳转到内容
主菜单
主菜单
移至侧栏
隐藏
导航
首页
最近更改
随机页面
MediaWiki帮助
代码酷
搜索
搜索
中文(中国大陆)
外观
创建账号
登录
个人工具
创建账号
登录
未登录编辑者的页面
了解详情
贡献
讨论
编辑“︁
Kotlin数据类型
”︁
页面
讨论
大陆简体
阅读
编辑
编辑源代码
查看历史
工具
工具
移至侧栏
隐藏
操作
阅读
编辑
编辑源代码
查看历史
常规
链入页面
相关更改
特殊页面
页面信息
外观
移至侧栏
隐藏
您的更改会在有权核准的用户核准后向读者展示。
警告:
您没有登录。如果您进行任何编辑,您的IP地址会公开展示。如果您
登录
或
创建账号
,您的编辑会以您的用户名署名,此外还有其他益处。
反垃圾检查。
不要
加入这个!
= Kotlin数据类型 = Kotlin是一种静态类型语言,所有变量在编译时都必须具有明确的类型。Kotlin的数据类型分为两大类:'''基本数据类型'''和'''引用数据类型'''。本节将详细介绍Kotlin中的数据类型及其特性。 == 基本数据类型 == Kotlin的基本数据类型包括数值类型、字符类型和布尔类型。尽管Kotlin是面向对象的语言,但为了提高性能,编译器会将基本数据类型优化为JVM的原始类型(Primitive Types)。 === 数值类型 === Kotlin提供以下数值类型: {| class="wikitable" |+ 数值类型及其范围 ! 类型 !! 大小(比特) !! 最小值 !! 最大值 |- | <code>Byte</code> || 8 || -128 || 127 |- | <code>Short</code> || 16 || -32768 || 32767 |- | <code>Int</code> || 32 || -2<sup>31</sup> || 2<sup>31</sup>-1 |- | <code>Long</code> || 64 || -2<sup>63</sup> || 2<sup>63</sup>-1 |- | <code>Float</code> || 32 || 1.4E-45 || 3.4028235E38 |- | <code>Double</code> || 64 || 4.9E-324 || 1.7976931348623157E308 |} ==== 示例代码 ==== <syntaxhighlight lang="kotlin"> fun main() { val byteValue: Byte = 127 val shortValue: Short = 32767 val intValue = 2147483647 // 类型推断为Int val longValue = 9223372036854775807L // 后缀L表示Long val floatValue = 3.14F // 后缀F表示Float val doubleValue = 3.1415926535 // 默认是Double println("Byte: $byteValue") println("Short: $shortValue") println("Int: $intValue") println("Long: $longValue") println("Float: $floatValue") println("Double: $doubleValue") } </syntaxhighlight> 输出: <pre> Byte: 127 Short: 32767 Int: 2147483647 Long: 9223372036854775807 Float: 3.14 Double: 3.1415926535 </pre> === 字符类型 === <code>Char</code>表示单个16位Unicode字符,用单引号表示。 <syntaxhighlight lang="kotlin"> val letter: Char = 'A' val unicodeChar: Char = '\u0041' // Unicode表示'A' </syntaxhighlight> === 布尔类型 === <code>Boolean</code>表示逻辑值,只有<code>true</code>和<code>false</code>两个值。 <syntaxhighlight lang="kotlin"> val isTrue: Boolean = true val isFalse = false // 类型推断 </syntaxhighlight> == 引用数据类型 == Kotlin中的所有非基本类型都是引用类型,包括: * 字符串(<code>String</code>) * 数组(<code>Array</code>) * 集合(如<code>List</code>, <code>Set</code>, <code>Map</code>) * 自定义类 === 字符串 === Kotlin中的字符串是不可变的,用双引号表示。支持模板表达式和原始字符串(三重引号)。 <syntaxhighlight lang="kotlin"> val name = "Kotlin" val message = "Hello, $name!" // 字符串模板 val multiLine = """ This is a multi-line string """.trimIndent() </syntaxhighlight> === 数组 === Kotlin数组用<code>Array</code>类表示,创建方式有多种: <syntaxhighlight lang="kotlin"> val numbers = arrayOf(1, 2, 3) // 使用arrayOf函数 val zeros = Array(3) { 0 } // 大小为3,初始化为0 val intArray = intArrayOf(1, 2, 3) // 基本类型数组 </syntaxhighlight> == 类型转换 == Kotlin不支持隐式类型转换,必须显式转换: <syntaxhighlight lang="kotlin"> val intValue: Int = 100 val longValue: Long = intValue.toLong() // 显式转换 </syntaxhighlight> == 可空类型 == Kotlin的类型系统区分可空和非空引用,通过在类型后加<code>?</code>表示可空: <syntaxhighlight lang="kotlin"> var nullableString: String? = null // 可空字符串 var nonNullString: String = "Hello" // 非空字符串 </syntaxhighlight> == 类型检查与智能转换 == 使用<code>is</code>操作符检查类型,编译器会自动智能转换: <syntaxhighlight lang="kotlin"> fun printLength(obj: Any) { if (obj is String) { println(obj.length) // obj自动转换为String } } </syntaxhighlight> == 实际应用案例 == === 用户信息处理 === <syntaxhighlight lang="kotlin"> data class User( val id: Int, val name: String, val age: Int? // 年龄可为null ) fun processUser(user: User) { val ageDescription = user.age?.let { "Age: $it" } ?: "Age not provided" println("${user.name}, $ageDescription") } </syntaxhighlight> === 温度转换 === <syntaxhighlight lang="kotlin"> fun convertTemperature(value: Double, from: String, to: String): Double { return when { from == "C" && to == "F" -> value * 9/5 + 32 from == "F" && to == "C" -> (value - 32) * 5/9 else -> throw IllegalArgumentException("Unsupported conversion") } } </syntaxhighlight> == 类型关系图 == <mermaid> graph TD A[Kotlin数据类型] --> B[基本数据类型] A --> C[引用数据类型] B --> D[数值类型] B --> E[Char] B --> F[Boolean] D --> G[Byte, Short, Int, Long] D --> H[Float, Double] C --> I[String] C --> J[Array] C --> K[集合] C --> L[自定义类] </mermaid> == 数学表示 == Kotlin数值类型的范围可以用数学表达式表示: * <math>\text{Byte} \in [-128, 127]</math> * <math>\text{Int} \in [-2^{31}, 2^{31}-1]</math> * <math>\text{Double} \in [4.9\times10^{-324}, 1.7976931348623157\times10^{308}]</math> == 总结 == Kotlin的数据类型系统设计既安全又高效,通过区分基本类型和引用类型、支持可空类型检查等特性,帮助开发者编写更健壮的代码。理解这些数据类型及其转换规则是掌握Kotlin编程的基础。 [[Category:编程语言]] [[Category:Kotlin]] [[Category:Kotlin基础]]
摘要:
请注意,所有对代码酷的贡献均被视为依照知识共享署名-非商业性使用-相同方式共享发表(详情请见
代码酷:著作权
)。如果您不希望您的文字作品被随意编辑和分发传播,请不要在此提交。
您同时也向我们承诺,您提交的内容为您自己所创作,或是复制自公共领域或类似自由来源。
未经许可,请勿提交受著作权保护的作品!
取消
编辑帮助
(在新窗口中打开)