当前位置:网站首页>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 // 编译通过
边栏推荐
- Take you two minutes to quickly master the route and navigation of flutter
- Matlab label2idx function (convert the label matrix into a cell array with linear index)
- Understanding the architecture type of mobile CPU
- Matlab imoverlay function (burn binary mask into two-dimensional image)
- About cache exceptions: solutions for cache avalanche, breakdown, and penetration
- What is digital existence? Digital transformation starts with digital existence
- Experimental design - using stack to realize calculator
- mysql拆分字符串做条件查询
- Time tools
- Resnet+attention project complete code learning
猜你喜欢
Course design of compilation principle --- formula calculator (a simple calculator with interface developed based on QT)
Flutter2 heavy release supports web and desktop applications
Learn memory management of JVM 01 - first memory
One article tells the latest and complete learning materials of flutter
Seven ways to achieve vertical centering
Interviewer: is acid fully guaranteed for redis transactions?
Redis highly available sentinel mechanism
NPM install reports an error
嵌入式软件架构设计-消息交互
Keras implements verification code identification
随机推荐
Matlab struct function (structure array)
Design of music box based on assembly language
MySQL data table operation DDL & data type
[figure neural network] GNN from entry to mastery
Take you hand in hand to develop a service monitoring component
信息服务器怎么恢复,服务器数据恢复怎么弄[通俗易懂]
什么是数字化存在?数字化转型要先从数字化存在开始
Differences between IPv6 and IPv4 three departments including the office of network information technology promote IPv6 scale deployment
Anaconda creates a virtual environment and installs pytorch
Xi IO flow
Learn the memory management of JVM 03 - Method area and meta space of JVM
NPM install reports an error
Why learn harmonyos and how to get started quickly?
Instance + source code = see through 128 traps
One article tells the latest and complete learning materials of flutter
Seven ways to achieve vertical centering
Redis highly available sentinel mechanism
Redis highly available sentinel cluster
Take you two minutes to quickly master the route and navigation of flutter
MySQL stored procedure