当前位置:网站首页>Kotlin basics 1
Kotlin basics 1
2022-07-06 01:09:00 【hzulwy】
var & val & Type derivation
val a = "Brett";
a = "Jett"// This is not allowed , The compiler will report an error
When using val When variables are declared , Variables cannot be changed after being assigned initial values ,val Declared variable is read-only . If you want to change the value of a variable, you can set val Change it to var. Put the above code to val Change it to var Then the compiler will not report an error .
As you can see from the above code ,kotlin Variables of can not declare types , because kotlin There is a type derivation mechanism . The compiler in the above code can automatically deduce a yes String type . Not only can variables not declare types , Functions can also not declare types , for example :
fun a(a:Int){
return 10;// The function can automatically deduce the type of the function as Int
}
kotlin And java There is another difference in the method of :java Methods of must be declared in the class , however kotlin Functions of can be declared inside and outside the class .
Here comes a question of thinking :kotlin Is it a static language or a dynamic language ? The answer is static language . because kotlin The type of is determined by the compiler , All languages that determine the type in the compiler are static languages . If you don't believe me, you can try , Put the above code to a=10, At this time, the compiler will report an error , because a The initial value is String type .
const val
Compilation is constant , Only applicable to kotlin Common basic data types :(String、Double、Int、Float、Long、Short、Byte、Char、Boolean)
Compile time constants can only be defined outside functions : Because if it is defined in a function , You must assign values at runtime , Why compile time constants .
const val PI= 3.14
fun main(){
const val a = 12// This is a mistake
}
Here comes a question :(Byte、Char、Boolean、Short、Long、Float、Int、Double) yes kotlin Class , It belongs to the reference data type , Will it affect performance ? The answer is no , because kotlin The code will eventually turn into java Basic type
Null pointer check
Compare with java Null pointer check for ,kotlin Two ways are added .
val a = "Brett";
a?.length//a by null, Don't execute length Method
a!!.length// No matter what a Is it null, It will be carried out length Method , And java Agreement
if(a != null){
// This kind of java The same as
a.lenth
}
Built in data types
String | character string |
Char | A single character |
Int | integer |
Float/Double | floating-point |
List | aggregate |
Set | No duplicate element set |
Map | Set of key value pairs |
when expression
//when amount to java in switch, however when Is an expression , You can have a return value
//info The type of Any, amount to java Of Object.Unit amount to java Of void, however void Is a keyword and Unit Is a class
val info= when(x){
1,2 -> "xxxx"
3 -> "aaaa"
else{
println("xxxx")
}
}
if expression
kotlin Of if It's an expression, not a statement , You can have a return value , The return value is the last sentence .
fun main(){
val isLogin = false;
println("${
if (isLogin) congratulations ,else No, congratulations } ")
}
function
The significance of the existence of named functions lies in , When passing parameters , It is not necessary to pass in the order of parameters .
fun main(){
show(-90)
}
private fun show(number:Int){
when(number){
in 0..59 -> println(" Failing grades ")
in 60..100 -> println(" Pass the grade ")
else -> TODO(" There is no such score ")//Nothing type , Will terminate the program and throw an exception .
}
}
// Backquote function
public class test{
public static void is (){
System.out.println("is")
}
}
fun main(){
// First usage
` Test the backquote function `()
// Second usage
test.`is`()
}
private fun ` Test the backquote function `(){
println("test")
}
边栏推荐
- [groovy] XML serialization (use markupbuilder to generate XML data | set XML tag content | set XML tag attributes)
- 【第30天】给定一个整数 n ,求它的因数之和
- Synchronized and reentrantlock
- Construction plan of Zhuhai food physical and chemical testing laboratory
- 测试/开发程序员的成长路线,全局思考问题的问题......
- Mlsys 2020 | fedprox: Federation optimization of heterogeneous networks
- Leetcode study - day 35
- After 95, the CV engineer posted the payroll and made up this. It's really fragrant
- [groovy] compile time meta programming (compile time method interception | method interception in myasttransformation visit method)
- Study diary: February 13, 2022
猜你喜欢
Recursive method to realize the insertion operation in binary search tree
Cve-2017-11882 reappearance
看抖音直播Beyond演唱会有感
关于#数据库#的问题:(5)查询库存表中每本书的条码、位置和借阅的读者编号
Installation and use of esxi
cf:H. Maximal AND【位运算练习 + k次操作 + 最大And】
从 1.5 开始搭建一个微服务框架——调用链追踪 traceId
Who knows how to modify the data type accuracy of the columns in the database table of Damon
Free chat robot API
Four commonly used techniques for anti aliasing
随机推荐
MIT博士论文 | 使用神经符号学习的鲁棒可靠智能系统
Distributed base theory
SAP Spartacus home 页面读取 product 数据的请求的 population 逻辑
Live video source code, realize local storage of search history
Gartner发布2022-2023年八大网络安全趋势预测,零信任是起点,法规覆盖更广
Use of crawler manual 02 requests
Five challenges of ads-npu chip architecture design
Gartner released the prediction of eight major network security trends from 2022 to 2023. Zero trust is the starting point and regulations cover a wider range
[groovy] compile time meta programming (AST syntax tree conversion with annotations | define annotations and use groovyasttransformationclass to indicate ast conversion interface | ast conversion inte
详细页返回列表保留原来滚动条所在位置
MYSQL---查询成绩为前5名的学生
Illustrated network: the principle behind TCP three-time handshake, why can't two-time handshake?
What is the most suitable book for programmers to engage in open source?
Spark AQE
在产业互联网时代,将会凭借大的产业范畴,实现足够多的发展
Daily practice - February 13, 2022
[Arduino syntax - structure]
How to extract MP3 audio from MP4 video files?
VMware Tools安装报错:无法自动安装VSock驱动程序
The detailed page returns to the list and retains the original position of the scroll bar