当前位置:网站首页>The first day of a solid foundation for Kotlin
The first day of a solid foundation for Kotlin
2022-08-08 20:33:00 【Do not forget the original intention z】
The first day of a solid kotlin foundation
Quickly query data in database classes, such as finding the largest number
First define a data class
data class Person(val name:String, val age:Int? =null) where?Indicates that this data can be empty, this is an important knowledge point that must be kept in mind!
Now use this database class to put
fun main(args:Array{
fun person = listOf(Person(“Zhang”),Person(“Zhao”,15))
}
Among them, we created a list collection, which stores two data classes. Note that the constructor of one of the data classes has only one name, because the second function we set when we created the data class can be empty., so if you don't enter a value, the system will create a variable with an age of null for us by default
Now compare the size of the two data classes age
val oldest =person.maxBy(it.age ?: 0) where ?: is the Elvis operator, which means that if there is no value, it will be replaced with 0, so Zhao is the maximum age
oldest is 15
Simple writing of functions, such as passing in two values to compare the maximum value
fun max(a : Int , b : Int) :Int {
return if(a>b) a else b
}
The above function is to compare the size of an Int value, but the function title of this function is composed of a single expressionIf so, you can use this expression as the complete function body, without the curly braces and return statement
fun max (a :Int , b : Int ) :Int = if (a > b) a else b If the function body is written inside curly braces, then the function has a code block, if it directly returns an expressionformula, then it is given to the body of the expression
About the difference between variable modifiers val var
Methods of declaring variables in java
String name = "Zhang San" It is necessary to declare that this variable is of type String, and if the variable needs to be immutable, a final constant needs to be added, but it is completely omitted in kotlin, just write
val name = "Zhang San" There is no need to apply for a variable type, because the kotlin compiler will deduce that this is a String type and add the val modifier, then this variable is immutable.However, one concept to understand is that the reference itself (name) is immutable, but the object it points to is mutable, such as pointing to a collection type, then the collection type is mutable, such as:
val list =arrayListOf("1")
list.add("5") This code is perfectly valid
var age = "5" A variable decorated with var is mutable, allowing to change its amount, butIt is not allowed to change its type; for example: var age = "5" | var age = "Zhangsan" This approach is wrong because its type is immutable.
Simpler string template
The output string in kotlin is println("Hello Kotlin") You can also use $(variable) in println, for example:
val name = "Kotlin" println("Hello ${name}") NoteThe point is that if there is no more character after $, you can remove the curly braces
Classes and Properties
To create a data class in java, we often need to create get and set methods for variables.But in Kotlin it is not needed.
class Person(val name:String, var age: Int) Kotlin will automatically create a get method for the name variable, because the name is val modified, then it has only one get method, but will create a get method for the var modified age variableand set method.
边栏推荐
- 虚假信息处理最新有何进展?KDD2022《打击错误信息和应对媒体偏见》教程,161页ppt
- Kotlin study notes
- Notes: The difference between laravel, updateOrCreate and updateOrInsert
- com.alibaba.fastjson.JSONException: default constructor not found. class
- XTU OJ 1075 求最小公倍数
- IJCAI 2022 | Can Graph Neural Networks Detect Anomalies?
- Ansible自动化运维工具(二)playbook剧本
- From interview to autism, five rounds of interviews for byte software testing post, four hours of soul torture...
- 暑期“小候鸟”超员增多 惠州交警提醒:安全出行不能忘
- Kotlin-学习的第五天之Handler
猜你喜欢
随机推荐
正则表达式与文本处理器
Categorized input and output, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, go lang basic data types and input and output EP03
内网渗透之代理转发
记一次坎坷的调试|Mosquitto通过TLS连接EMQ时阻塞的问题
SushiSwap「SUSHI」下降了 93%,但还没有完全消失
Float.parseFloat()的作用
Kotlin-学习的第五天之Handler
CVPR 2022 ActivityNet竞赛冠军:中科院深圳先进院提出高低分双模态行为识别框架
1259 Alice and Bob
uni-app微信小程序如何渲染markdown
LeetCode_67_二进制求和
Kotlin注解
实践篇1:深度学习之----LetNet之tensorflow2的实现
正则表达式的限定符、或运算符、字符类、元字符、贪婪/懒惰匹配
投资基金定投安全吗
买股票安全吗 资金能取出来吗
Kotlin study notes
瑞吉外卖项目实战Day06--手机端
NAACL2022 NER SOTA—RICON学习笔记
1100 三角形面积







![Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception](/img/95/1041a1c23d020c272ca345d87019b2.png)

