Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the similarities and differences between Kotlin and Java

Shulou Source: shulou.com Published: 2022-06-02 19:39:36 10月02日 Update

This article mainly introduces "what are the similarities and differences between Kotlin and Java". In daily operation, I believe many people have doubts about the similarities and differences between Kotlin and Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what are the similarities and differences between Kotlin and Java?" Next, please follow the editor to study!

Print log

Java

System.out.print ("hello world")

System.out.println ("hello world")

Kotlin

Print ("hello world")

Println ("hello world")

Define variables and constants

Java

String name = "hello world"

Final String name = "hello world"

Kotlin

Var name = "hello world"

Val name = "hello world"

Null statement

Java

String otherName

OtherName = null

Kotlin

Var otherName: String?

OtherName = null

Empty judgment

Java

If (text! = null) {

Int length = text.length ()

}

Kotlin

Text?.let {

Val length = text.length

}

/ / or simply

Val length = text?.length

String concatenation

Java

String firstName = "Android"

String lastName = "Architect"

String message = "My name is:" + firstName + "" + lastName

Kotlin

Val firstName = "Android"

Val lastName = "Architect"

Val message = "My name is: $firstName $lastName"

New line

Java

String text = "First Line\ n" +

"Second Line\ n" +

"Third Line"

Kotlin

Val text = ""

| | First Line |

| | Second Line |

| | Third Line |

"" .trimMargin ()

Ternary expression

Java

String text = x > 5? "x > 5": "x 5"

Else "x

Tags: Similarities and differences learning methods more types constructors help utility next variables characters strings constants operators articles logs conditions theory knowledge symbols Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Linux macOS Xiaomi Apple