当前位置:网站首页>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)
}
}
边栏推荐
- An example of multi module collaboration based on NCF
- Unity adds a function case similar to editor extension to its script, the use of ContextMenu
- PointNeXt:通过改进的模型训练和缩放策略审视PointNet++
- 在线SQL转Excel(xls/xlsx)工具
- Functional interface
- 长城证券开户安全吗 买股票怎么开户
- Swagger突然发癫
- Jetpack Compose 教程
- 牛客小白月赛7 F题
- 1006 sign in and sign out (25 points) (PAT class a)
猜你喜欢
Online sql to excel (xls/xlsx) tool
Summary and sorting of 8 pits of redis distributed lock
线上数据库迁移的几种方法
To sort out messy header files, I use include what you use
Comment utiliser async awati asynchrone Task Handling au lieu de backgroundworker?
Wireshark网络抓包
一文掌握数仓中auto analyze的使用
2022CoCa: Contrastive Captioners are Image-Text Fountion Models
Pointnet/Pointnet++点云数据集处理并训练
如何使用Async-Awati异步任務處理代替BackgroundWorker?
随机推荐
Online sql to excel (xls/xlsx) tool
Leetcode fizzbuzz C # answer
安徽 中安在线文旅频道推出“跟着小编游安徽”系列融媒体产品
Introduction to polyfit software
In flinksql, in addition to data statistics, is the saved data itself a state
1672. 最富有客户的资产总量
QT realizes interface sliding switching effect
There are multiple divs in the large div, which are displayed on the same line. After overflow, scroll bars are generated without line breaks
Online text line fixed length fill tool
BCG 使用之CBCGPProgressDlg进度条使用
Shell 编程核心技术《三》
Personal thoughts on Architecture Design (this article will be revised and updated continuously later)
Have you guys ever used CDC direct Mysql to Clickhouse
Lm10 cosine wave homeopathic grid strategy
The 15th youth informatics competition in Shushan District in 2019
Wireshark网络抓包
HDU 1372 & POJ 2243 Knight moves (breadth first search)
牛客小白月赛7 I 新建 Microsoft Office Word 文档
Specify the character set to output
92.(cesium篇)cesium楼栋分层