菜单
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Kotlin Data Types


Kotlin 数据类型

In Kotlin, the type of a variable is decided by its value

示例

val myNum = 5             // Int
val myDoubleNum = 5.99    // Double
val myLetter = 'D'        // Char
val myBoolean = true      // Boolean
val myText = "Hello"      // String
自己动手试一试 »

However, you learned from the previous chapter that it is possible to specify the type if you want

示例

val myNum: Int = 5                // Int
val myDoubleNum: Double = 5.99    // Double
val myLetter: Char = 'D'          // Char
val myBoolean: Boolean = true     // Boolean
val myText: String = "Hello"      // String
自己动手试一试 »

Sometimes you have to specify the type, and often you don't. Anyhow, it is good to know what the different types represent.

You will learn more about when you need to specify the type later.

Data types are divided into different groups

  • 数字
  • 字符
  • 布尔值
  • 字符串
  • 数组

数字

Number types are divided into two groups

Integer types store whole numbers, positive or negative (such as 123 or -456), without decimals. Valid types are Byte, Short, Int and Long.

Floating point types represent numbers with a fractional part, containing one or more decimals. There are two types: Float and Double.

If you don't specify the type for a numeric variable, it is most often returned as Int for whole numbers and Double for floating point numbers.


整数类型

Byte

The Byte data type can store whole numbers from -128 to 127. This can be used instead of Int or other integer types to save memory when you are certain that the value will be within -128 and 127

示例

val myNum: Byte = 100
println(myNum)
自己动手试一试 »

Short

The Short data type can store whole numbers from -32768 to 32767

示例

val myNum: Short = 5000
println(myNum)
自己动手试一试 »

Int

The Int data type can store whole numbers from -2147483648 to 2147483647

示例

val myNum: Int = 100000
println(myNum)
自己动手试一试 »

Long

The Long data type can store whole numbers from -9223372036854775808 to 9223372036854775807. This is used when Int is not large enough to store the value. Optionally, you can end the value with an "L"

示例

val myNum: Long = 15000000000L
println(myNum)
自己动手试一试 »

Difference Between Int and Long

A whole number is an Int as long as it is up to 2147483647. If it goes beyond that, it is defined as Long

示例

val myNum1 = 2147483647  // Int
val myNum2 = 2147483648  // Long


浮点数类型

Floating point types represent numbers with a decimal, such as 9.99 or 3.14515.

The Float and Double data types can store fractional numbers

Float 示例

val myNum: Float = 5.75F
println(myNum)
自己动手试一试 »

Double 示例

val myNum: Double = 19.99
println(myNum)
自己动手试一试 »

Use Float or Double?

The precision of a floating point value indicates how many digits the value can have after the decimal point. The precision of Float is only six or seven decimal digits, while Double variables have a precision of about 15 digits. Therefore it is safer to use Double for most calculations.

Also note that you should end the value of a Float type with an "F".

科学计数法

A floating point number can also be a scientific number with an "e" or "E" to indicate the power of 10

示例

val myNum1: Float = 35E3F
val myNum2: Double = 12E4
println(myNum1)
println(myNum2)
自己动手试一试 »

布尔值

The Boolean data type and can only take the values true or false

示例

val isKotlinFun: Boolean = true
val isFishTasty: Boolean = false
println(isKotlinFun)   // Outputs true
println(isFishTasty)   // Outputs false 
自己动手试一试 »

布尔值主要用于条件测试,你将在后面的章节中了解更多。


字符

The Char data type is used to store a single character. A char value must be surrounded by single quotes, like 'A' or 'c'

示例

val myGrade: Char = 'B'
println(myGrade)
自己动手试一试 »

Unlike Java, you cannot use ASCII values to display certain characters. The value 66 would output a "B" in Java, but will generate an error in Kotlin

示例

val myLetter: Char = 66
println(myLetter) // Error

字符串

The String data type is used to store a sequence of characters (text). String values must be surrounded by double quotes

示例

val myText: String = "Hello World"
println(myText)
自己动手试一试 »

You will learn more about strings in the Strings chapter.


数组

数组用于在单个变量中存储多个值,而不是为每个值声明单独的变量。

You will learn more about arrays in the Arrays chapter.


类型转换

Type conversion is when you convert the value of one data type to another type.

In Kotlin, numeric type conversion is different from Java. For example, it is not possible to convert an Int type to a Long type with the following code

示例

val x: Int = 5
val y: Long = x
println(y) // Error: Type mismatch 
自己动手试一试 »

To convert a numeric data type to another type, you must use one of the following functions: toByte(), toShort(), toInt(), toLong(), toFloat(), toDouble() or toChar()

示例

val x: Int = 5
val y: Long = x.toLong()
println(y)
自己动手试一试 »


×

联系销售

如果您想将 W3Schools 服务用于教育机构、团队或企业,请发送电子邮件给我们
sales@w3schools.com

报告错误

如果您想报告错误,或想提出建议,请发送电子邮件给我们
help@w3schools.com

W3Schools 经过优化,旨在方便学习和培训。示例可能经过简化,以提高阅读和学习体验。教程、参考资料和示例会不断审查,以避免错误,但我们无法保证所有内容的完全正确性。使用 W3Schools 即表示您已阅读并接受我们的使用条款Cookie 和隐私政策

版权所有 1999-2024 Refsnes Data。保留所有权利。W3Schools 由 W3.CSS 提供支持