当前位置:网站首页>kotlin 基本使用
kotlin 基本使用
2022-07-04 18:32:00 【AdleyTales】
fun main() {
println("hello world, Hello Kotlin ~")
val result = sum(12, 5)
println(result) // 17
val result2 = sum2(12, 5)
println(result2) // 17
val result3 = sum2(12, 5)
println(result3) // 17
varsFunc("aaaa", "bbbb", "cccc", "dddd", "郄朋亮")
println("******------******------******------******------******------******------")
// 定义常量和变量 val同java的final
val firstname = "adley"
val lastname = "tales"
// 字符串模板
val str = "My name is $firstname $lastname !"
println(str)
// NULL检查机制
// 空安全 字段后加!!像Java一样抛出空异常,另一种字段后加?可不做处理返回值为 null 或者 ?: 做空判断处理
// 类型后面加?表示可为空
var name: String? = "adley"
name = null
println(name)
// 抛出空指针异常
// var age = name!!.toInt()
// 不做处理返回 null
val age2 =name?.toInt()
println(age2) // null
val ages3 = age2?.toInt() ?: -1 // 如果是空,则为-1
println(ages3) // -1
println(name is String) // false
println(str is String) // true
// 区间
for (i in 1..5) {
println(i)
}
for (i in 1..5 step 2) {
println(i)
}
}
// 基本用法
// 包声明: package com.adleytales.demo
// 函数定义
fun sum(x: Int, y: Int): Int {
return x + y
}
// 函数
fun sum2(x: Int, y: Int): Int = x + y
// 函数 lambda
val sum3: (Int, Int) -> Int = {
x, y -> x + y }
// 可变长参数 vararg
fun varsFunc(vararg str: String) {
for (s in str) {
println(s)
}
}
边栏推荐
猜你喜欢

升级智能开关,“零火版”、“单火”接线方式差异有多大?

与二值化阈值处理相关的OpenCV函数、方法汇总,便于对比和拿来使用
关于判断点是否位于轮廓内的一点思考

How to use async Awati asynchronous task processing instead of backgroundworker?

mysql中explain语句查询sql是否走索引,extra中的几种类型整理汇总
Some thoughts on whether the judgment point is located in the contour

PointNeXt:通过改进的模型训练和缩放策略审视PointNet++

To sort out messy header files, I use include what you use

"Only one trip", active recommendation and exploration of community installation and maintenance tasks

Explore the contour drawing function drawcontours() of OpenCV in detail with practical examples
随机推荐
Guys, for help, I use MySQL CDC 2.2.1 (Flink 1.14.5) to write Kafka and set
The kth largest element in the array
How test engineers "attack the city" (Part 2)
1672. Total assets of the richest customers
偏移量函数及开窗函数
Generate XML elements
项目中遇到的线上数据迁移方案1---总体思路整理和技术梳理
Bi skills - permission axis
测试工程师如何“攻城”(下)
Double colon function operator and namespace explanation
prometheus安装
Summary and sorting of 8 pits of redis distributed lock
Online text line fixed length fill tool
HDU 6440 2018 Chinese college student program design network competition
添加命名空间声明
勾股数规律(任意三个数能够满足勾股定理需要满足的条件)
Personal thoughts on Architecture Design (this article will be revised and updated continuously later)
2014 Hefei 31st youth informatics Olympic Games (primary school group) test questions
Online sql to excel (xls/xlsx) tool
Qt实现界面滑动切换效果