当前位置:网站首页>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
边栏推荐
猜你喜欢
idea将web项目打包成war包并部署到服务器上运行
Eureka self protection
QT OpenGL rotate, pan, zoom
Alibaba is bigger than sending SMS (user microservice - message microservice)
OpenGL index cache object EBO and lineweight mode
剑指Offer09. 用两个栈实现队列
Self made pop-up input box, input text, and click to complete the event.
Display time with message interval of more than 1 minute in wechat applet discussion area
Sword finger offer09 Implementing queues with two stacks
Shutter widget: centerslice attribute
随机推荐
elastic_ L02_ install
C language improvement article (wchar_t) character type
OpenGL index cache object EBO and lineweight mode
2020-10_ Development experience set
实现验证码验证
Develop plug-ins for idea
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?
242. Effective letter heteronyms
idea将web项目打包成war包并部署到服务器上运行
ES6 standard
Use bloc to build a page instance of shutter
Flinksql can directly create tables and read MySQL or Kafka data on the client side, but how can it automatically flow and calculate?
Integer string int mutual conversion
Use of atomicinteger
023 ([template] minimum spanning tree) (minimum spanning tree)
记录自己vulnhub闯关记录
Slf4j log facade
Unicode查询的官方网站
temp
Flutter: self study system