--Kotlin is a language that runs on the JVM created by JetBrains. --Kotlin is fully interoperable with Java. You can write a class in Java, a class that inherits it in Kotlin, and a class that inherits it in Java. --You can write additional development in Kotlin for a project written in Java, which makes it easy to introduce. --Google has announced official Kotlin support for Android.
Use val for constants and var for mutable variables.
test.java
String str = "String";
final int value = 0;
test.kt
var str: String = "String"
val value = 0
Since type inference is possible in Kotlin, variables can be defined by omitting the type. No semicolon is needed.
test.java
String value = bar < 10 ? "abc" : "xyz";
test.kt
var value = if (bar < 10) "abc" else "xyz"
You can directly assign the result in the if or else block.
test.java
for(int num : numbers)
{
System.out.println(num);
}
for(int i = 0; i< 4; i++)
{
System.out.println(i);
}
test.kt
for (num in numbers)
{
println(num)
}
for (i in 0..3)
{
println(i)
}
Kotlin uses in.
test.java
public int add(int a, int b)
{
return a + b;
}
add(1,2);
test.kt
fun add(a:Int, b:Int): Int {
return a + b
}
//Caller
add(1,2)
In the case of Kotlin, if the access modifier is omitted, it is treated as public. Furthermore, if the return type allows the compiler to perform type inference, it is possible to omit the definition of the return type.