当前位置:网站首页>kotlin基础语法
kotlin基础语法
2022-06-29 16:27:00 【伟雪无痕】
一.Kotlin 基本数据类型
1.Kotlin 的基本数值类型包括 Byte、Short、Int、Long、Float、Double 等。不同于 Java 的是,字符不属于数值类型,是一个独立的数据类型,eg:如下表
| 类型 | 位宽度 | 占用字节数 |
|---|---|---|
| Double | 64 | 8 |
| Float | 32 | 4 |
| Long | 64 | 8 |
| Int | 32 | 4 |
| Short | 16 | 2 |
| Byte | 8 | 1 |
数据类型转换方式,eg:
toByte(): 转换到Byte类型
toShort(): 转换到Short类型
toInt(): 转换到Int类型
toLong(): 转换到Long类型
toFloat(): 转换到Float类型
toDouble(): 转换到Double类型
toChar(): 转换到Char类型
2.定义一些常量和变量的
1).可变变量定义:var 关键字
var <标识符> : <类型> = <初始化值>2).不可变变量定义:val 关键字,只能赋值一次的变量(类似Java中final修饰的变量)
val <标识符> : <类型> = <初始化值> var m: Int=30
var flag:Boolean=true
val value1 : Int=200
val value2: Double=30.5备注: java和kotlin变量的区别
1'.位置不同,kotlin中数据类型放到变量的后面,并用冒号(:)分隔,与java相反
2'.关键不同,kotlin中定义变量用var开头,定义常量用val开头
3'.数字类型首字母大小写不同,kotlin中数据类型都以大写字母开头,java中只有类、接口等复杂的数据类型的名字才采用首字母大写的方式
4'.kotlin中常量与变量都可以没有初始化值,但是在引用前必须初始化,编译器支持自动类型判断,即声明时可以不指定类型,由编译器判断
二.包声明,和java一样,代码文件的开头一般为包的声明,且Kotlin 文件以 .kt 为后缀,eg:
package com.example.kotlin
class PersonTest(val name:String) {
fun greet(){
println("Hi,$name")
}
var m: Int=30
var flag:Boolean=true
val value1 : Int=200
val value2: Double=30.5
}
fun main(args:Array<String>){
PersonTest("kotlin!").greet()
}如果没有指定包,默认为 default 包,有多个包会默认导入到每个 Kotlin 文件中:
kotlin.*
kotlin.annotation.*
kotlin.collections.*
kotlin.comparisons.*
kotlin.io.*
kotlin.ranges.*
kotlin.sequences.*
kotlin.text.*
三.函数定义, 函数定义使用关键字 fun,参数格式为:参数 : 类型,其中函数的变长参数可以用 vararg 关键字进行标识,eg:
fun sum(a:Int,b:Int):Int{
return a+b
}
fun getName():String{
return "jon"
}
fun vars(vararg values:Int){
for (value in values){
println(value)
}
}四.注释方式,支持单行注释和块注释
单行注释
// 这是一个单行注释
块注释
/* Hi kotlin */五.字符串模板,模板使用美元符号($)开头,eg:"i=$i“中的$i就是一个占位符,其中$后面的i是变量,随着i变化,eg:
val i=100
val j="i=$i" //相当于"i=100"
println(j)
val k="1234"
var str="$k length is ${k.length}" //使用字符串获取k的长度
println(str)六.数组
//使用函数arrayOf()
val arr1= arrayOf(1,2,3,'a',4)
println(arr1[3])
arr1[2]='b'
println(arr1[2])
var arr2=Array(5,{i->(i*i).toString()})
println(arr2[3])
//使用IntArray数组,省去了装箱操作,因此效率更高,其用法同Array一样,另外也可以定义ByteArray, ShortArray和IntArray定义类似
var arr3:IntArray= intArrayOf(2,4,6,8,10)
println("arr3[2]="+arr3[2])边栏推荐
- Sophon base 3.1 launches mlops function to provide wings for enterprise AI capability operation
- Perhaps in two years, ASML will be free to supply EUV lithography machines to China
- Practice | solution for image upload, rotation and compression on mobile terminal
- [proteus simulation] progressive increase / decrease of nixie tube with flashing blanking display
- 自己实现一个ThreadLocal
- 南京大学:新时代数字化人才培养方案探讨
- MySQL基础——事务
- 一个简单但是能上分的特征标准化方法
- Mathematical knowledge: finding combinatorial number II - finding combinatorial number
- 破解湖+仓混合架构顽疾,星环科技推出自主可控云原生湖仓一体平台
猜你喜欢

Mathematical knowledge: finding combinatorial number II - finding combinatorial number

GNN notes: message propagation model

Stable currency risk profile: are usdt and usdc safe?

华夏基金:基金行业数字化转型实践成果分享

Profil de risque de monnaie stable: l'usdt et l'USDC sont - ils sûrs?

UWB precise positioning scheme, centimeter level high-precision technology application, intelligent pairing induction technology

Apache atlas breakpoint view

Accelerate the implementation of intelligent driving projects? You still lack a truth evaluation system

稳定币风险状况:USDT 和 USDC 安全吗?

InheritableThreadLocal 在线程池中进行父子线程间消息传递出现消息丢失的解析
随机推荐
『计组』CPU 如何区分指令和数据
分片信息调哪个参数呢?用的是MySQLsource stream api,不是table api
STM32按键消抖——入门状态机思维
Profil de risque de monnaie stable: l'usdt et l'USDC sont - ils sûrs?
Advanced webgl performance optimization
Take another picture of cloud redis' improvement path
Tool chain empowers hundreds of companies, horizon opens the "Matthew effect" of mass production of intelligent driving
Sophon base 3.1 launches mlops function to provide wings for enterprise AI capability operation
或许再过两年,ASML将可以自由给中国供应EUV光刻机
毕业生迷茫,中年人焦虑,职场路怎么越走越宽?
Sophon Base 3.1 推出MLOps功能,为企业AI能力运营插上翅膀
GNN notes: message propagation model
图文带你彻底弄懂MySQL事务原子性之UndoLog
Self taught programming can understand the code, but what if you can't write it yourself
Notice on organizing the declaration of Nanjing innovative products (the first batch) in 2022
Review of mathematical knowledge: curve integral of type I
Flutter技术与实战(2)
数学知识复习:第一型曲线积分
贪婪的苹果计划提高iPhone14的价格,这将为中国手机提供机会
Greedy Apple plans to raise the price of iphone14, which will provide opportunities for Chinese mobile phones