如何做到高级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
}
相关推荐
zmd-zk2 分钟前
flink学习(2)——wordcount案例
大数据·开发语言·学习·flink
好奇的菜鸟6 分钟前
Go语言中的引用类型:指针与传递机制
开发语言·后端·golang
Alive~o.015 分钟前
Go语言进阶&依赖管理
开发语言·后端·golang
花海少爷17 分钟前
第十章 JavaScript的应用课后习题
开发语言·javascript·ecmascript
手握风云-18 分钟前
数据结构(Java版)第二期:包装类和泛型
java·开发语言·数据结构
喵叔哟38 分钟前
重构代码中引入外部方法和引入本地扩展的区别
java·开发语言·重构
尘浮生44 分钟前
Java项目实战II基于微信小程序的电影院买票选座系统(开发文档+数据库+源码)
java·开发语言·数据库·微信小程序·小程序·maven·intellij-idea
hopetomorrow1 小时前
学习路之PHP--使用GROUP BY 发生错误 SELECT list is not in GROUP BY clause .......... 解决
开发语言·学习·php
小牛itbull1 小时前
ReactPress vs VuePress vs WordPress
开发语言·javascript·reactpress
请叫我欧皇i1 小时前
html本地离线引入vant和vue2(详细步骤)
开发语言·前端·javascript