当前位置:网站首页>Kotlin变量
Kotlin变量
2022-07-05 12:32:00 【好奇的菜鸟】
一、变量定义
/*
关键字 变量类型
↓ ↓ */
var price: Int = 100; /*
↑ ↑
变量名 变量值 */
大部分情况下,变量类型都可以推导出来,可以省略;末尾的分号一般省略;
二、变量的类型
package com.example.demo
fun main() {
// 数值类型
val int = 1
val long = 1234567L
val double = 13.14
val float = 13.14F
val hexadecimal = 0xAF
val binary = 0b01010101
// toByte()、toShort()、toInt()、toLong()、toFloat()、toDouble()、toChar()类型转换。
val j = int.toLong();
// 布尔类型
val flag1 = true
val flag2 = false
println(flag1 && flag2)
// 字符
val c: Char = 'A'
// 字符串
val s = "Hello Kotlin!"
val name = "Kotlin"
print("Hello $name!") //Hello Kotlin!
val array = arrayOf("Java", "Kotlin")
print("Hello ${array.get(1)}!") //Hello Kotlin!
val s2 = """
当我们的字符串有复杂的格式时
原始字符串非常的方便
因为它可以做到所见即所得。 """
print(s2)
// 数组
val arrayInt = arrayOf(1, 2, 3)
val arrayString = arrayOf("apple", "pear")
println(arrayInt.size) //3
}
三、空安全
val i: Double = null // 编译器报错
Kotlin 强制要求开发者在定义变量的时候,指定这个变量是否可能为 null。对于可能为 null 的变量,我们需要在声明的时候,在变量类型后面加一个问号“?”
var i: Double = 1.0
var j: Double? = null
i = j // 编译器报错
j = i // 编译通过
边栏推荐
- Pytoch uses torchnet Classerrormeter in meter
- Pytoch monolayer bidirectional_ LSTM implements MNIST and fashionmnist data classification
- Tabbar configuration at the bottom of wechat applet
- Distributed solution - Comprehensive decryption of distributed task scheduling platform -xxljob
- Redis highly available slice cluster
- Array cyclic shift problem
- Pytoch implements tf Functions of the gather() function
- byte2String、string2Byte
- Understanding the architecture type of mobile CPU
- The evolution of mobile cross platform technology
猜你喜欢
Read and understand the rendering mechanism and principle of flutter's three trees
Constructing expression binary tree with prefix expression
How can beginners learn flutter efficiently?
MySQL index - extended data
Redis highly available slice cluster
Understand kotlin from the perspective of an architect
Tabbar configuration at the bottom of wechat applet
Pytoch implements tf Functions of the gather() function
MySQL index (1)
Resnet18 actual battle Baoke dream spirit
随机推荐
Halcon template matching actual code (I)
Pytoch loads the initialization V3 pre training model and reports an error
MySQL splits strings for conditional queries
ZABBIX agent2 installation
语义分割实验:Unet网络/MSRC2数据集
Image hyperspectral experiment: srcnn/fsrcnn
MySQL stored procedure
A new WiFi option for smart home -- the application of simplewifi in wireless smart home
byte2String、string2Byte
UNIX socket advanced learning diary -ipv4-ipv6 interoperability
ZABBIX 5.0 - LNMP environment compilation and installation
The relationship between the size change of characteristic graph and various parameters before and after DL convolution operation
Take you two minutes to quickly master the route and navigation of flutter
Redis highly available sentinel mechanism
Distributed solution - Comprehensive decryption of distributed task scheduling platform -xxljob
Get all stock data of big a
Learn the memory management of JVM 02 - memory allocation of JVM
Cypher syntax of neo4j graph database
SENT协议译码的深入探讨
Video networkstate property