Kotlin 對比 轉化
Print
Java
System.out.print("Hello, World!");
System.out.println("Hello, World!");
Kotlin
print("Hello, World!")
println("Hello, World!")
Variables I
Java
final int x;
final int y = 1;
Kotlin
val x: Int
val y = 1
Variables II
Java
int w;
int z = 2;
z = 3;
w = 1;
Kotlin
var w: Int
var z = 2
z = 3
w = 1
Null I
Java
final String name = null;
String lastName;
lastName = null
Kotlin
val name: String? = null
var lastName: String?
lastName = null
var firstName: String
firstName = null // Compilation error!!
Null II
Java
if(text != null){
int length = text.length();
}
Kotlin
val length = text?.length
val length = text!!.length // NullPointerException if text == null
Strings I
Java
String name = "John";
String lastName = "Smith";
String text = "My name is: " + name + " " + lastName;
String otherText = "My name is: " + name.substring(2);
Kotlin
val name = "John"
val lastName = "Smith"
val text = "My name is: $name $lastName"
val otherText = "My name is: ${name.substring(2)}"
Strings II
Java
String text = "First Line\n" +
"Second Line\n" +
"Third Line";
Kotlin
val text = """
|First Line
|Second Line
|Third Line
""".trimMargin()
Ternary Operator
Java
String text = x > 5 ? "x > 5" : "x <= 5";
Kotlin
val text = if (x > 5)
"x > 5"
else "x <= 5"
BASICS
Bits Operations
Java
final int andResult = a & b;
final int orResult = a | b;
final int xorResult = a ^ b;
final int rightShift = a >> 2;
final int leftShift = a << 2;
Kotlin
val andResult = a and b
val orResult = a or b
val xorResult = a xor b
val rightShift = a shr 2
val leftShift = a shl 2
Is As In
Java
if(x instanceof Integer){ }
final String text = (String) other;
if(x >= 0 && x <= 10 ){}
Kotlin
if (x is Int) { }
val text = other as String
if (x in 0..10) { }
Smart Cast
Java
if(a instanceof String){
final String result = ((String) a).substring(1);
}
Kotlin
if (a is String) {
val result = a.substring(1)
}
Switch / When
Java
final int x = // value;
final String xResult;
switch (x){
case 0:
case 11:
xResult = "0 or 11";
break;
case 1:
case 2:
//...
case 10:
xResult = "from 1 to 10";
break;
default:
if(x < 12 && x > 14) {
xResult = "not from 12 to 14";
break;
}
if(isOdd(x)) {
xResult = "is odd";
break;
}
xResult = "otherwise";
}
final int y = // value;
final String yResult;
if(isNegative(y)){
yResult = "is Negative";
} else if(isZero(y)){
yResult = "is Zero";
}else if(isOdd(y)){
yResult = "is Odd";
}else {
yResult = "otherwise";
}
Kotlin
val x = // value
val xResult = when (x) {
0, 11 -> "0 or 11"
in 1..10 -> "from 1 to 10"
!in 12..14 -> "not from 12 to 14"
else -> if (isOdd(x)) { "is odd" } else { "otherwise" }
}
val y = // value
val yResult = when {
isNegative(y) -> "is Negative"
isZero(y) -> "is Zero"
isOdd(y) -> "is odd"
else -> "otherwise"
}
For
Java
for (int i = 1; i < 11 ; i++) { }
for (int i = 1; i < 11 ; i+=2) { }
for (String item : collection) { }
for (Map.Entry<String, String> entry: map.entrySet()) { }
Kotlin
for (i in 1..10) { }
for (i in 1..10 step 2) {}
for (item in collection) {}
for ((index, item) in collection.withIndex()) {}
for ((key, value) in map) {}
Collections
Java
final List<Integer> numbers = Arrays.asList(1, 2, 3);
final Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "One");
map.put(2, "Two");
map.put(3, "Three");
// Java 9
final List<Integer> numbers = List.of(1, 2, 3);
final Map<Integer, String> map = Map.of(1, "One",
2, "Two",
3, "Three");
Kotlin
val numbers = listOf(1, 2, 3)
val map = mapOf(1 to "One",
2 to "Two",
3 to "Three")
Collections
Java
for (int number : numbers) {
System.out.println(number);
}
for (int number : numbers) {
if(number > 5) {
System.out.println(number);
}
}
Kotlin
numbers.forEach {
println(it)
}
numbers.filter { it > 5 }
.forEach { println(it) }
Collections
Java
final Map<String, List<Integer>> groups = new HashMap<>();
for (int number : numbers) {
if((number & 1) == 0){
if(!groups.containsKey("even")){
groups.put("even", new ArrayList<>());
}
groups.get("even").add(number);
continue;
}
if(!groups.containsKey("odd")){
groups.put("odd", new ArrayList<>());
}
groups.get("odd").add(number);
}
Kotlin
val groups = numbers.groupBy {
if (it and 1 == 0) "even" else "odd"
}
Collections
Java
final List<Integer> evens = new ArrayList<>();
final List<Integer> odds = new ArrayList<>();
for (int number : numbers){
if ((number & 1) == 0) {
evens.add(number);
}else {
odds.add(number);
}
}
Kotlin
val (evens, odds) = numbers.partition { it and 1 == 0 }
Collections
Java
final List<User> users = getUsers();
Collections.sort(users, new Comparator<User>(){
public int compare(User user, User otherUser){
return user.lastname.compareTo(otherUser.lastname);
}
});
// or
users.sort(Comparator.comparing(user -> user.lastname));
Kotlin
val users = getUsers()
users.sortedBy { it.las
相關文章
- Kotlin 與 Java 對比KotlinJava
- XTask與Kotlin Coroutine的使用對比Kotlin
- Kotlin和Java的簡單對比KotlinJava
- [譯] 管中窺豹:RxJava 與 Kotlin 協程的對比RxJavaKotlin
- [Android元件化]Kotlin的路由跳轉Android元件化Kotlin路由
- 模組化通訊方式對比
- 對比C++和Java (轉)C++Java
- kotlin 集合內元素比較Kotlin
- Java陣列轉列表方式對比Java陣列
- 系統比對[oscmp.pl](轉)
- 對比 Redis 中 RDB 和 AOF 持久化Redis持久化
- 對比歸一化和標準化 —— 量化分析
- (轉)開源IT監控系統對比
- 不同平臺上mysql的對比(轉)MySql
- 【轉載】oracle的io優化--db_writer_processes & dbwr_io_slaves對比Oracle優化
- 轉向Kotlin——物件Kotlin物件
- 虛擬化平臺效能對比(KVM & VMware)
- 虛擬化技術對比:Xen vs KVM
- 對比Html和Flash網頁構成 (轉)網頁
- # Kotlin使用優化(四)Kotlin優化
- 轉向Kotlin——泛型Kotlin泛型
- Kotlin基礎:用Kotlin約定簡化相親Kotlin
- 程式導向和麵向物件的對比(轉)物件
- Java、C#執行緒模型分析對比 (轉)JavaC#執行緒模型
- Java中對HashMap的深度分析與比較(轉)JavaHashMap
- Kotlin Json 序列化KotlinJSON
- map 對比
- 轉向Kotlin——類和介面Kotlin
- 移動端 UI 自動化測試框架對比UI框架
- 外掛化踩坑之路——Small和Atlas方案對比
- ORACLE同步軟體技術實現對比(轉載)Oracle
- 全自動化資料洞察!資料分佈對比視覺化!⛵視覺化
- 再見數字化轉型:對數字化轉型的再思考
- 視覺化經典模型的對比實驗總結視覺化模型
- android kotlin dp 轉換工具AndroidKotlin
- AI模型對比AI模型
- 為什麼Kotlin比任何愚蠢的語言更好Kotlin
- Kubernetes 幾種儲存方式效能對比 (轉載)