当前位置:网站首页>Day 1 of kotlin learning: simple built-in types of kotlin
Day 1 of kotlin learning: simple built-in types of kotlin
2022-07-03 12:33:00 【Android little white star】
kotlin The first day of study
Learn video address :https://coding.imooc.com/class/398.html
1. Realization Java call Kotlin file
1. modify build.gradle Of Project file
Add code :
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.50'

2. modify build.gradle Of Module file
id 'kotlin-android'

implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.3.50'

So you can do it Java project , Use kotlin 了 .

2. hold java The file is converted to kotlin file
1. Click on code Below connect Java File to kotlin File

2. Conversion success

3.kotlin Basic type
1. Basic types

2. Declare variables
val And var The difference between :val Read only cannot be modified ,var Readable and modifiable .val And Java Medium final It's kind of like 
3.kotlin And Java The difference between :
1. Don't end with a comma ;
2. Type after , Variable name before ;
3. : The type can be saved .
4. modification Long Type , You can only add Capitalization L, Cannot use lowercase l.
val c=2000L √
val c=2000l ×
5.Kotlin Numeric type conversion for
No direct conversion , Need to add to type ().
example :
// take Int Type into Long type
val e:Int=10
val f:Long=e.toLong()
6.kotlin Unsigned type , And c In language unsigned The same is true .
example :
fun main()
{
val g:UInt= 2u // Unsigned type
println("$g")
}
7.kotlin Output
val name=" Li Hua "
println("name is :$name")
println("length is :${name.length}") // When there is a way , Need to add {}
System.out.printf("length is :%d",name.length)

8.kotlin Comparison of address and value in
use === The comparison values are equal , And Java Medium == The effect is the same
use == Compare address equality , And Java Medium equals The effect is the same
9.kotlin Output multiple lines of values , Not like Java Same use \n Line break , And with + To splice .kotlin It is more convenient .
var test=""" 123 123 """.trimIndent()
println(" Output :\n$test") //trimIndent Method : Used to cut the same number of spaces at the beginning of each line

4. Array

1. Array creation
val c0= intArrayOf(1,2,3,4,5)
val c1=IntArray(5){
2*(it+1)}
println(c0.contentToString()) //contentToString Method to print the array
println(c1.contentToString())

2. Length of array
val a=IntArray(5);
println(a.size)
3. Array read and write
val d= arrayOf("Hello","World")
d[1]="Kotlin"
println("${
d[0]},${
d[1]}")

4. Traversal of array
val e= floatArrayOf(1f,3f,5f,7f)
// Method 1
for(value in e)
{
println(value)
}
// Method 2
e.forEach {
element->
println(element)
}
// Method 3
val array= intArrayOf(1,3,5,7)
for (i in array.indices) //indices Get the interval [0,4)
{
println(array[i])
}

5. Section
1. The creation of intervals
val intRange=1..10 // Closed interval [1,10]
val charRange='a' until 'd' // Front closing back opening section [a,d)
val longRange=10L downTo 1L // Inverted interval
val intRangeStep=1..10 step 2 // step step Set to 2
val floatRange=1f..10f // The above is discrete value , Here is the continuous value
val doubleRange=1.0..10.0
println(intRange.joinToString())
println(charRange.joinToString())
println(longRange.joinToString())
println(intRangeStep.joinToString())
println(floatRange.toString())
println(doubleRange.toString())

2. Simple traversal
Same as array method
6. Collections framework
1. Introduction to collection framework
Collection framework reuse Java API All implementations of 
Immutable List Elements cannot be added or deleted .
example :
val intList: List<Int> = listOf(1,2,3)
val intList2: MutableList<Int> = mutableListOf(1,2,3)
val map:Map<String,Any> = mutableMapOf("name" to "luo","age" to 20)//Any Equivalent to Java Of Object
val stringList=ArrayList<String>()
2. Modification of collection framework
// Add data
for(i in 0..10){
stringList+="num:$i" // Equivalent to stringList.add("num:$i")
}
println(stringList.toString())
// Delete data
for (i in 0..3)
{
stringList-="num:$i" // Equivalent to stringList.remove("num:$i")
}
println(stringList.toString())

3. The reading and writing of set frame
stringList[5]="HelloWorld" // stay stringList Add data
val valueAt5=stringList[5] // stay stringList Reading data
println(valueAt5)
val hashmap=HashMap<String,Int>()
hashmap["Hello"]=10
println(hashmap["Hello"])
4.Pair( Two elements )
// Two ways to create
val pair ="Hello" to "Kotlin"
val pair2=Pair("Hello","Kotlin")
// Get the corresponding element
val first=pair.first
val second=pair.second
val (x,y)=pair
5.Triple( Three elements )
// establish Triple
val triple=Triple("x",2,3.0)
// Get the corresponding element
val first1=triple.first
val second1=triple.second
val third1=triple.third
val (x1,y1,z1)=triple
7. function
1. Function definition

Unit amount to Java Of void, It can be omitted
2. Function reference


(Foo,String,Long)->Any Equivalent to Foo.(String,Long)->Any Equivalent to (Foo,String,Long,Any)
3. Variable parameters
multiParameters(1,2,3,4) // Set parameters
//vararg Set variable parameters , The number of parameters is variable
fun multiParameters(vararg ints:Int)
{
println(ints.contentToString())
}

4. Multiple return values
val (x2,y2,y3)= multiReturnValues() // deconstruction
fun multiReturnValues():Triple<Int,Long,Double>
{
return Triple(1,3L,4.0)
}
5. The default function
defaultParameter(2) // among y,z All have default values
fun defaultParameter(x:Int,y:String=" Xiao Ming ",z:Double=20.0)
{
TODO()
}
6. Named functions
The difference from the default function :defaultParameter(y=" Li Hua "), You can set the place to assign , Because the default function generally defaults to the first function
边栏推荐
- Introduction to concurrent programming (I)
- wpa_ cli
- temp
- 023 ([template] minimum spanning tree) (minimum spanning tree)
- elastic_ L01_ summary
- Implement verification code verification
- 242. Effective letter heteronyms
- 2.7 overview of livedata knowledge points
- Eureka自我保护
- 02_ Lock the code, and don't let the "lock" become a worry
猜你喜欢

Self made pop-up input box, input text, and click to complete the event.

【附下载】密码获取工具LaZagne安装及使用

ES6新特性

剑指Offer03. 数组中重复的数字【简单】

Public and private account sending prompt information (user microservice -- message microservice)

使用BLoC 构建 Flutter的页面实例

Pki/ca and digital certificate

With pictures and texts, summarize the basic review of C language in detail, so that all kinds of knowledge points are clear at a glance?

C language improvement article (wchar_t) character type

LeetCode 0556. Next bigger element III - end of step 4
随机推荐
242. Effective letter heteronyms
RedHat5 安装Socket5代理服务器
(construction notes) learn the specific technology of how to design reusable software entities from three levels: class, API and framework
PHP export word method (phpword)
Dart: about Libraries
[official MySQL document] deadlock
Flutter 退出登录二次确认怎么做才更优雅?
(construction notes) learning experience of MIT reading
剑指Offer05. 替换空格
What is more elegant for flutter to log out and confirm again?
Sword finger offer03 Repeated numbers in the array [simple]
Swagger
【嵌入式】---- 内存四区介绍
Dart: About zone
Atomic atomic operation
Public and private account sending prompt information (user microservice -- message microservice)
(构造笔记)GRASP学习心得
(构造笔记)ADT与OOP
Wechat applet - basic content
Take you to the installation and simple use tutorial of the deveco studio compiler of harmonyos to create and run Hello world?