当前位置:网站首页>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 // 编译通过
边栏推荐
- MySQL data table operation DDL & data type
- Take you hand in hand to develop a service monitoring component
- Pytoch loads the initialization V3 pre training model and reports an error
- [hdu 2096] Xiaoming a+b
- Understanding the architecture type of mobile CPU
- Get data from the database when using JMeter for database assertion
- Introduction to GNN
- MySQL regular expression
- Course design of compilation principle --- formula calculator (a simple calculator with interface developed based on QT)
- Average lookup length when hash table lookup fails
猜你喜欢
Detailed structure and code of inception V3
Principle of universal gbase high availability synchronization tool in Nanjing University
Hexadecimal conversion summary
Automated test lifecycle
Course design of compilation principle --- formula calculator (a simple calculator with interface developed based on QT)
NPM install reports an error
Learn memory management of JVM 01 - first memory
Redis's memory elimination mechanism, read this article is enough.
Read and understand the rendering mechanism and principle of flutter's three trees
MySQL storage engine
随机推荐
Instance + source code = see through 128 traps
Take you two minutes to quickly master the route and navigation of flutter
Four operations and derivative operations of MATLAB polynomials
Anaconda creates a virtual environment and installs pytorch
Why learn harmonyos and how to get started quickly?
Redis clean cache
July Huaqing learning-1
Distributed solution - distributed lock solution - redis based distributed lock implementation
Learn JVM garbage collection 02 - a brief introduction to the reference and recycling method area
A new WiFi option for smart home -- the application of simplewifi in wireless smart home
MySQL stored procedure
MySQL multi table operation
MySQL basic operation -dql
Read and understand the rendering mechanism and principle of flutter's three trees
Pytoch monolayer bidirectional_ LSTM implements MNIST and fashionmnist data classification
Learn garbage collection 01 of JVM -- garbage collection for the first time and life and death judgment
How to design an interface?
struct MySQL
ZABBIX agent2 installation
PIP command reports an error pip is configured with locations that requires tls/ssl problems