当前位置:网站首页>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 // 编译通过边栏推荐
- Swift - add navigation bar
- 语义分割实验:Unet网络/MSRC2数据集
- Matlab imoverlay function (burn binary mask into two-dimensional image)
- MySQL regular expression
- Experimental design - using stack to realize calculator
- [superhard core] is the core technology of redis
- Understanding the architecture type of mobile CPU
- Handwriting blocking queue: condition + lock
- How can beginners learn flutter efficiently?
- NPM install reports an error
猜你喜欢

NPM install reports an error

Matlab superpixels function (2D super pixel over segmentation of image)
Take you two minutes to quickly master the route and navigation of flutter

UNIX socket advanced learning diary -ipv4-ipv6 interoperability

Matlab label2idx function (convert the label matrix into a cell array with linear index)

The evolution of mobile cross platform technology

Understand redis persistence mechanism in one article

Knowledge representation (KR)

Master the new features of fluent 2.10

Understand kotlin from the perspective of an architect
随机推荐
语义分割实验:Unet网络/MSRC2数据集
Detailed structure and code of inception V3
Matlab superpixels function (2D super pixel over segmentation of image)
Get data from the database when using JMeter for database assertion
The evolution of mobile cross platform technology
How does MySQL execute an SQL statement?
Handwriting blocking queue: condition + lock
NPM install reports an error
Keras implements verification code identification
MySQL log module of InnoDB engine
Design of music box based on assembly language
Seven polymorphisms
struct MySQL
Solve the error 1045 of Navicat creating local connection -access denied for user [email protected] (using password
Distributed solution - completely solve website cross domain requests
MySQL multi table operation
Constructing expression binary tree with prefix expression
Distributed cache architecture - cache avalanche & penetration & hit rate
A new WiFi option for smart home -- the application of simplewifi in wireless smart home
Learn the memory management of JVM 02 - memory allocation of JVM