当前位置:网站首页>Kotlin basic grammar
Kotlin basic grammar
2022-06-29 16:43:00 【Great snow without trace】
One .Kotlin Basic data type
1.Kotlin The basic numerical types of are Byte、Short、Int、Long、Float、Double etc. . differ Java Yes. , Character is not of numerical type , Is a separate data type ,eg: The following table
| type | Bit width | Bytes occupied |
|---|---|---|
| Double | 64 | 8 |
| Float | 32 | 4 |
| Long | 64 | 8 |
| Int | 32 | 4 |
| Short | 16 | 2 |
| Byte | 8 | 1 |
Data type conversion method ,eg:
toByte(): The switch to Byte type
toShort(): The switch to Short type
toInt(): The switch to Int type
toLong(): The switch to Long type
toFloat(): The switch to Float type
toDouble(): The switch to Double type
toChar(): The switch to Char type
2. Defining some constants and variables
1). Variable definition :var keyword
var < identifier > : < type > = < initialize value >2). Definition of immutable variable :val keyword , Variables that can only be assigned once ( similar Java in final Decorated variable )
val < identifier > : < type > = < initialize value > var m: Int=30
var flag:Boolean=true
val value1 : Int=200
val value2: Double=30.5remarks : java and kotlin The difference between variables
1'. Different location ,kotlin Put the data type in after the variable , And use a colon (:) Separate , And java contrary
2'. The key is different ,kotlin To define variables in var start , Define constants with val start
3'. The first letter of the number type has different case ,kotlin All data types in begin with capital letters ,java There are only classes in 、 The names of complex data types such as interfaces are capitalized
4'.kotlin Both constants and variables in can have no initialization value , But you must initialize... Before referencing , The compiler supports automatic type determination , That is, you can declare without specifying the type , Judged by the compiler
Two . Package declaration , and java equally , The beginning of the code file is usually the declaration of the package , And Kotlin Document to .kt For the suffix ,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()
}If there is no specified package , The default is default package , Multiple packages will be imported into each by default Kotlin In file :
kotlin.*
kotlin.annotation.*
kotlin.collections.*
kotlin.comparisons.*
kotlin.io.*
kotlin.ranges.*
kotlin.sequences.*
kotlin.text.*
3、 ... and . Function definition , The function definition uses the keyword fun, The parameter format is : Parameters : type , The variable length parameter of the function can be vararg Keywords to identify ,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)
}
}Four . Annotation mode , Support single line annotation and block annotation
Single-line comments
// This is a single line comment
Block annotation
/* Hi kotlin */5、 ... and . String template , The template uses the dollar sign ($) start ,eg:"i=$i“ Medium $i It's a placeholder , among $ hinder i It's a variable. , With i change ,eg:
val i=100
val j="i=$i" // amount to "i=100"
println(j)
val k="1234"
var str="$k length is ${k.length}" // Use string to get k The length of
println(str)6、 ... and . Array
// Using functions 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])
// Use IntArray Array , No packing operation , So it's more efficient , Its usage is the same as Array equally , You can also define ByteArray, ShortArray and IntArray The definition is similar to
var arr3:IntArray= intArrayOf(2,4,6,8,10)
println("arr3[2]="+arr3[2])边栏推荐
- 真正的测试 =“半个产品+半个开发”?
- 实战 | Change Detection And Batch Update
- Graduates are confused and middle-aged people are anxious. How can the career path become wider and wider?
- iNFTnews | Meta在元宇宙中的后续计划会是什么?
- Comment configurer logback? 30 minutes pour apprendre à coder et à frapper tard.
- 元代理模型可迁移对抗攻击
- MySQL进阶——存储引擎
- 6.26CF模拟赛D:黑白条题题解
- 一个简单但是能上分的特征标准化方法
- 如何配置 logback?30分鐘讓你徹底學會代碼熬夜敲
猜你喜欢
随机推荐
2022年有哪些适合穷人的理财产品?
英联邦国家有哪些
A simple but scalable feature normalization method
MySQL foundation - transaction
都3年测试经验了,用例设计还不知道状态迁移法?
Locust performance pressure test tool
SAAS服务都有哪些优势
分片信息调哪个参数呢?用的是MySQLsource stream api,不是table api
「BUAA OO Unit 4 HW16」第四单元总结与课程回顾
What are the financial products suitable for the poor in 2022?
[proteus simulation] 8-bit nixie tube dynamic scanning display change data
南京大学:新时代数字化人才培养方案探讨
实战 | Change Detection And Batch Update
About xampp unable to start MySQL database
[untitled]
星环科技数据安全管理平台 Defensor重磅发布
Us chips are hit hard again, and Intel may be defeated by TSMC and reduced to the third place in the world
How to configure logback? 30 minutes for you to thoroughly learn the code to stay up late and knock
A tour of gRPC:02 - 从proto生成代码
毕业生迷茫,中年人焦虑,职场路怎么越走越宽?





![[untitled]](/img/e2/be57a7e22275af59183c50e0710837.png)



