如何做到高级Kotlin强化实战?(二)

高级Kotlin强化实战(二)

        • [2.Kotlin 与 Java 比较](#2.Kotlin 与 Java 比较)

2.Kotlin 与 Java 比较

2.5 字符串拼接

java 复制代码
//Java
String firstName = "Android";
String lastName = "enginner";
String message = "My name is: " + firstName + " " + lastName;
kotlin 复制代码
//Kotlin
val firstName = "Android"
val lastName = "enginner"
val message = "My name is: $firstName $lastName"

2.6 三元表达式

java 复制代码
//Java
String text = x > 5 ? "x > 5" : "x <= 5";
kotlin 复制代码
//kotlin
val text = if (x > 5)
"x > 5"
else
"x <= 5"

2.7 多重条件

java 复制代码
//Java
if (score >= 0 && score <= 300) { }
java 复制代码
//kotlin
if (score in 0..300) { }

2.8 更灵活的 case 语句

java 复制代码
//Java
int score = // some score;
String grade;
switch (score) {
case 10:
case 9:
grade = "Excellent";
break;
case 8:
case 7:
case 6:
grade = "Good";
break;
case 5:
case 4:
grade = "OK";
break;
case 3:
case 2:
case 1:
grade = "Fail";
break;
default:
grade = "Fail";
}
kotlin 复制代码
//kotlin
var score = // some score
var grade = when (score) {
9, 10 -> "Excellent"
in 6..8 -> "Good"
4, 5 -> "OK"
in 1..3 -> "Fail"
else -> "Fail"
}

2.9 for 循环

java 复制代码
//Java
for (int i = 1; i <= 10 ; i++) { }
for (int i = 1; i < 10 ; i++) { }
for (int i = 10; i >= 0 ; i--) { }
for (int i = 1; i <= 10 ; i+=2) { }
for (int i = 10; i >= 0 ; i-=2) { }
for (String item : collection) { }
for (Map.Entry<String, String> entry: map.entrySet()) { }
kotlin 复制代码
//kotlin
for (i in 1..10) { }
for (i in 1 until 10) { }
for (i in 10 downTo 0) { }
for (i in 1..10 step 2) { }
for (i in 10 downTo 0 step 2) { }
for (item in collection) { }
for ((key, value) in map) { }

2.10 更方便的集合操作

java 复制代码
//Java
final List<Integer> listOfNumber = Arrays.asList(1, 2, 3, 4);
final Map<Integer, String> keyValue = new HashMap<Integer, String>();
map.put(1, "Android");
map.put(2, "Ali");
map.put(3, "Mindorks");
// Java 9
final List<Integer> listOfNumber = List.of(1, 2, 3, 4);
final Map<Integer, String> keyValue = Map.of(1, "Android",
2, "Ali",
3, "Mindorks");
kotlin 复制代码
//kotlin
val listOfNumber = listOf(1, 2, 3, 4)
val keyValue = mapOf(1 to "Android",
2 to "Ali",
3 to "Mindorks")

2.11 遍历

java 复制代码
// Java 7 and below
for (Car car : cars) {
System.out.println(car.speed);
}
// Java 8+
cars.forEach(car -> System.out.println(car.speed));
// Java 7 and below
for (Car car : cars) {
if (car.speed > 100) {
System.out.println(car.speed);
}
}
// Java 8+
cars.stream().filter(car -> car.speed > 100).forEach(car -> System.out.println(car.speed));
kotlin 复制代码
//kotlin
cars.forEach {
println(it.speed)
}
cars.filter { it.speed > 100 }
.forEach { println(it.speed)}

2.12 方法定义

java 复制代码
//Java
void doSomething() {
// logic here
}
void doSomething(int... numbers) {
// logic here
}
复制
kotlin 复制代码
//kotlin
fun doSomething() {
// logic here
}
fun doSomething(vararg numbers: Int) {
// logic here
}
相关推荐
可涵不会debug5 分钟前
C语言文件操作:标准库与系统调用实践
linux·服务器·c语言·开发语言·c++
百流42 分钟前
scala文件编译相关理解
开发语言·学习·scala
Evand J2 小时前
matlab绘图——彩色螺旋图
开发语言·matlab·信息可视化
深度混淆3 小时前
C#,入门教程(04)——Visual Studio 2022 数据编程实例:随机数与组合
开发语言·c#
雁于飞3 小时前
c语言贪吃蛇(极简版,基本能玩)
c语言·开发语言·笔记·学习·其他·课程设计·大作业
wenxin-4 小时前
NS3网络模拟器中如何利用Gnuplot工具像MATLAB一样绘制各类图形?
开发语言·matlab·画图·ns3·lr-wpan
数据小爬虫@6 小时前
深入解析:使用 Python 爬虫获取苏宁商品详情
开发语言·爬虫·python
健胃消食片片片片6 小时前
Python爬虫技术:高效数据收集与深度挖掘
开发语言·爬虫·python
王老师青少年编程7 小时前
gesp(C++五级)(14)洛谷:B4071:[GESP202412 五级] 武器强化
开发语言·c++·算法·gesp·csp·信奥赛