当前位置:网站首页>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)
}
}
边栏推荐
- Swagger突然发癫
- Allure of pytest visual test report
- Bi skills - permission axis
- Wechat reading notes of "work, consumerism and the new poor"
- 1672. Total assets of the richest customers
- 基于NCF的多模块协同实例
- Unity adds a function case similar to editor extension to its script, the use of ContextMenu
- HDU 1097 A hard puzzle
- 函数式接口
- YOLOv5s-ShuffleNetV2
猜你喜欢

Mysql database basic operation -ddl | dark horse programmer

Comment utiliser async awati asynchrone Task Handling au lieu de backgroundworker?

Explore the contour drawing function drawcontours() of OpenCV in detail with practical examples

西门子HMI下载时提示缺少面板映像解决方案

FPGA时序约束分享01_四大步骤简述

线上数据库迁移的几种方法

There are multiple divs in the large div, which are displayed on the same line. After overflow, scroll bars are generated without line breaks

与二值化阈值处理相关的OpenCV函数、方法汇总,便于对比和拿来使用

YOLOv5s-ShuffleNetV2

Introduction to polyfit software
随机推荐
爬虫(6) - 网页数据解析(2) | BeautifulSoup4在爬虫中的使用
BCG 使用之新建向导效果
TCP两次挥手,你见过吗?那四次握手呢?
【问题】druid报异常sql injection violation, part alway true condition not allow 解决方案
Introduction to polyfit software
页面元素垂直水平居中、实现已知或者未知宽度的垂直水平居中。
Unity editor extends C to traverse all pictures in folders and subdirectories
Shell 編程核心技術《四》
Jetpack Compose 教程
矩阵翻转(数组模拟)
如何使用Async-Awati异步任務處理代替BackgroundWorker?
The 300th weekly match of leetcode (20220703)
kotlin 条件控制
Reflection (I)
C# 使用StopWatch测量程序运行时间
Jetpack compose tutorial
Shell programming core technology "three"
node_exporter部署
LM10丨余弦波动顺势网格策略
The page element is vertically and horizontally centered, realizing the vertical and horizontal centering of known or unknown width.