当前位置:网站首页>Kotlin basic grammar
Kotlin basic grammar
2022-07-22 09:42:00 【CodingForAndroid】
Basic grammar : Defined function 、 Defining variables 、 Constant 、 Use conditional expressions
import java.lang.Integer.parseInt
//① Defined function
// One function accepts two int Type parameter , The return value is int :
fun sum1(a:Int ,b :Int):Int{
return a+b
}
// This function has only one expression function body and a self deriving return value :
fun sum2(a:Int,b:Int) = a+b
// Returns a meaningless value :
fun printSum(a:Int,b:Int):Unit{
println(a+b)
}
//Unit The return type of can be omitted
fun printSum2(a:Int,b:Int){
println(a+b)
}
//② Defining local variables
fun declareVariables(): Unit {
// declare constant :
val a: Int = 1
val b = 1 // Deduce Int type
val c: Int // When there is no initialization value, you must declare type
c = 1 // assignment
// Declare variables
var x = 5 // Deduce Int type
x += 1
}
//③ Use string templates
fun main(args:Array<String>){
if (args.size == 0) return
println("First argument: ${
args[0]}")
}
//④ Use conditional expressions
fun max(a:Int,b:Int):Int{
if(a>b)
return a
else
return b
}
// Or the if When the conditional expression
fun max2(a:Int,b:Int) = if(a>b)a else b
//⑤ Use nullable variables and null values to check
// When a null value may appear, it should be clearly indicated that the reference can be null .
// The following function is when str Returns NULL if it does not contain an integer :
fun parseInt(str:String):Int?{
//.....
}
// Use a function that returns nullable values :
fun main(args:Array<String>){
if(args.size<2){
print("length 2 expected")
return
}
val x = parseInt(args[0])
val y = parseInt(args[1])
// Use it directly x*y Errors will occur because there may be null values in them
if(x != null && y != null){
//x and y It will be automatically converted to non null value after null value detection
print(x*y)
}
// Or so
if (x == null) {
print("Wrong number format in '${
args[0]}' ")
return
}
if (y == null) {
print("Wrong number format in '${
args[1]}' ")
return
}
//x and y It will be automatically converted to non null value after null value detection
print(x * y)
}
边栏推荐
猜你喜欢
EAS BOS 自定义导出(含Excel样式设置、多页签导出、导出文件目录校验及备份)
[Datasheet ] 计量芯片CS5480数据手册解读
Measured 11 remote video conferencing software, home work can also be as smooth as silk
滚轮_阻止默认行为_滚零事件兼容
High frequency leetcode deep search part: Sword finger offer 36 Binary search tree and bidirectional linked list
2022年 iuap 春季培训数据中台培训报道
EAS Web BIM启动访问提示500错误
The error interface of EAS web page preview is blank
无套路、无陷阱、无广告 | 这个免费的即时通讯软件确定不用吗?
粘性定位(sticky)详解
随机推荐
SqlServer将数据库中的表复制到另一个数据库
Oracle函数大全之日期函数
原型继承
[Datasheet] PHY LAN8720网络芯片解读
Uni interceptor
High frequency leetcode deep search part: 124 Maximum path sum in binary tree
让盒子居中且距离浏览器左右各100px做动画
performance 优化
生物化学复习VII·糖代谢
File read / write operation (files under the specified file directory)
EAS BOS report development
Kotlin基础语法
生物化学复习VI·生物氧化
The error interface of EAS web page preview is blank
微信小程序Text限定行数
20201127 使用Markdown 画uml图,Graphviz 安装经历吐血整理
Common functions of EAS extended report (self use)
【Datasheet】PHY KSZ9031千兆网络芯片解读
call、apply、bind以及bind的兼容问题
生物化學複習題V·酶