KOtlin for java Developers a course by JetBrains
fun main(){ println("First ${foo()}, Second ${foo()}") }
fun foo():String{ println("calculating to....") return "foo" }
output
calculating to.... calculating to.... First foo, Second foo
kotlin variables
i val comes from value Read-Only correspondes final variable in java
ii var comes from variable , mutable variable
modification of val
val language = mutableListOf("Java") language.add("kotlin")
kotlin functions
fun checksmall(a :Int, b:Int):Int{ return if (a<b) a else b }
transformed to
fun checksmall(a : Int, b : Int):Int = if (a < b) a else b
println(listof('a','b','c').joinToString( separator="", prefix = "(",posyfix = ")")))
abc
println(listof(1,2,3).joinToString(posyfix = "."))
1,2.3.
fun displaySeparator(character : Char="*", size : Int = 10){ repeat(size){ print(character) } }
val max = if(a . b) a else b
String description;
switch(colour){
case BLUE: description = "Cold";
break;
case ORANGE: description = "Mid";
break;
}
val description = when(colour){
BLUE -> "Cold" ORANGE -> "Mid"
}
val a = listOf("a","b","c")
for(s in a){
print(s) }
val a = listOf("a","b","c")
for((index,element) in a.withIndex()){
print("$index : $element") }
val map = mapOf( 1 to "One", 2 to "Two", 3 to "Three")
for((key , value) in map){
println("$key = $value")
}
for(number in 1..10){
println(number)
}
for(number in 1 until 10){
println(number)
}
for( ch in "abc"){
print(ch + 1) }
for( number in 10 downTo1 step 2){
print(number)
}
public class Person {
private final String name;
private final int age;
public Person(String name,int age){
this.name = name;
this.age = age;
}
public String getname(){
return name;
}
public int getage(){
return age;
}
}
in Kotlin
class Person(val name:String, val age:Int)
Done, all the constructor and the getter all together
in java
public void updateweather(int degrees){
String description;
Colour colour;
if(degrees < 5){
description = "Cold";
colour = BLUE;
}else if(degrees < 23){
description = "MID";
colour = ORANGE;
}else{
description = "Cold";
colour = RED;
}
}
in kotlin use pair keyword
fun updateweather(degrees : Int){
val(description:String , colour:Colour)=
if(degrees < 5){
Pair("Cold",BLUE)
} else if(degrees < 23){
Pair("Mid",ORANGE)
} else {
Pair("HOt",RED)
}
}
transformed using when keyword
fun updateweather(degrees : Int){
val(description:String , colour:Colour)= when{
degrees < 5 -> Pair("Cold",BLUE)
degrees < 23 -> Pair("Mid",ORABGE)
else -> Pair("Hot",RED)
}
}
more short using to keyword
fun updateweather(degrees : Int){
val(description:String , colour:Colour)= when{
degrees < 5 -> "Cold" to BLUE
degrees < 23 -> "Mid" to ORABGE
else -> "Hot" to RED
}
}