Kotlin 基础(一)

Kotlin 基础

1: 日志打印

println 缓冲自动刷新. logcat 直接输出打印.

复制代码
println("Hello World!....123")

print 缓冲不刷新 logcat 中无输出.

解决办法:

  1. print 后增加flush(),手动刷新.

    复制代码
    print("Hello")
    print("111")
    System.out.flush()
  2. 手动添加换行

    复制代码
    print("Hello222\n")
    print("Hello333\n")

Log立即打印

复制代码
Log.d("Hello","world world")

test:

复制代码
/**
 * 函数:用于测试
 */
fun test() {
    println("Hello World!....123")
    print("Hello")
    print("111")
    System.out.flush()
    print("Hello222\n")
    print("Hello333\n")
    print("Hello444")
    Log.d("Hello","world world")

    var a = 1
    a = 2
    println(a)
    val b = 2
//    b= a
    println("Hello"+b)
}

输出如下:

2026-04-01 13:01:12.825 2990-2990 System.out com.zh.systemtest I Hello World!..123

2026-04-01 13:01:12.826 2990-2990 System.out com.zh.systemtest I Hello111

2026-04-01 13:01:12.826 2990-2990 System.out com.zh.systemtest I Hello222

2026-04-01 13:01:12.826 2990-2990 System.out com.zh.systemtest I Hello333

2026-04-01 13:01:12.826 2990-2990 Hello com.zh.systemtest D world world

2026-04-01 13:01:12.827 2990-2990 System.out com.zh.systemtest I Hello4442

2026-04-01 13:01:12.827 2990-2990 System.out com.zh.systemtest I Hello2

2: 变量

val (value) - 不可变引用,只能赋值一次

var (variable) - 可变引用,可以多次赋值

复制代码
var a = 1
println(a)

val b = 2
b= a //这块会直接报错.
println(b)

3: 类型转换

代码如下:

复制代码
fun main() {
    var a:Int = 1
    var b:Long = a //会报错
    println(b)
}

在 Kotlin 中,类型转换与java不同, 要将一种数值数据类型转换为另一种类型,你必须使用以下函数之一:toByte()toShort()toInt()toLong()toFloat()toDouble()toChar().

4: 字符串

复制代码
fun main() {
    var a:Int = 1
    var b:Long = a.toLong()
    println(b)
    var str1 = "hello"
    var str2 = "world"
    println(str1+str2)
    println(str1.plus(str2))
}

使用plus函数链接字符串.

除了连接之外,您还可以使用"字符串模板",这是一种在字符串内部添加变量和表达式的简单方法。

只需用 $ 符号引用变量即可:

复制代码
var str3 = "hello"
var str4 = "world"
println("this is $str3 $str4")

输出:

this is hello world

5: if... else

if 用作表达式时,您还必须包含 else(必需)。

复制代码
var i = 4
val str5 = if (i < 12) {
    "morning"
}else {
    "eat"
}
println(str5)

6: when

复制代码
val i = 4
val day = when (i) {
    1 -> "Monday"
    2 -> "Tuesday"
    3 -> "Wednesday"
    4 -> "Thursday"
    5 -> "Friday"
    6 -> "Saturday"
    7 -> "Sunday"
    else -> "Invalid day"
}
println(day)
相关推荐
Enaium13 小时前
我实现了KMP的SDL绑定并编写了高性能软光追
前端·kotlin
Android打工仔13 小时前
Continuation 的链式关系 —— 一个 suspend 函数如何把结果交还给调用者?
android·kotlin
Kapaseker15 小时前
小白都看得懂的 Skill 教程 - 初识 Skill
android·kotlin·vibecoding
zhangphil16 小时前
Android Coil 3 ImageRequest.Builder error() 和 fallback()异同
android·kotlin
我命由我123451 天前
Android 控件 - CardView(快速实现圆角)
android·java·java-ee·kotlin·android studio·android-studio·android runtime
大黄说说2 天前
Java 与 Kotlin 混合开发避坑指南:老项目平滑迁移 Kotlin 实操手册
java·开发语言·kotlin
我命由我123452 天前
Android Drawable - gradient
android·java·java-ee·kotlin·android studio·android-studio·android runtime
Kapaseker2 天前
AI 时代,读源码还有用吗?从 Kotlin 集合排序说起
android·kotlin
YB13752 天前
jetpack compose 副作用 SideEffect
android·kotlin
唐青枫2 天前
Kotlin interface 详解:别再把接口写成空壳和万能开关
kotlin