Take the second quiz on Spring framework and evaluate yourself. If you’ve missed the first one, here you go.
Kotlin Video Series – Functions (Part 1)
This is the seventh part of the video series on Kotlin language.
Read and watch the other parts here.
1. | Hello Kotlin |
2. | Declaring Variables |
3. | Data Types |
4. | String (Part 1) |
5. | String (Part 1) |
6. | Conditional Expressions |
We discuss writing functions in Kotlin language.
We’ve been writing a main function in all these examples. So that should have already given us some idea about the syntax of the functions. A Function is written using the fun keyword.
fun main(args:Array<String>){ println(sayHello("Sam")) } fun sayHello(name:String):String{ return "Hello $name" }
You can omit the return type of a function if doesn’t return anything.
fun print(message:String){ println(message) }
We can also make a function that does not return anything to return Unit, which means Nothing. Unit can be used in Generics, which we will discuss later.
fun printAgain(message:String):Unit{ println(message) }
Functions can be written as expressions, like our if-else and when-else constructs. An add function thats adds two numbers can be written as an expression like this.
//Normal function fun add(x:Int,y:Int):Int{ return x + y } //Expressions fun add(x:Int,y:Int) = x + y fun square(x:Int) = x * x }
You can find the video here.
Kotlin Video Series – Conditional Expressions
This is the sixth part of the video series on Kotlin language.
Read and watch the other parts here.
1. | Hello Kotlin |
2. | Declaring Variables |
3. | Data Types |
4. | String (Part 1) |
5. | String (Part 2) |
In this video, we discuss conditional expressions. Yes, it’s conditional expressions and not just conditional statements.
Let’s write a pretty simple if-else condition.
fun main(args:Array<String>){ val comment:String if(age > 50) comment = "Getting old" else comment = "You'll get there" println(comment) }
if-else conditions can be written as expressions in Kotlin. And that, eliminates the need for having a ternary operator.
//if-else used as an Expression val anotherComment = if(age > 50){ "Getting Old" } else{ "You'll get there" } println(anotherComment) //Even better val finalComment = if(age > 50) "Getting Old" else "You'll get there" println(finalComment)
The finalComment variable above just resembles our ternary operator. It’s pretty cool.
switch-case statements are available as when statements in Kotlin.
val age = 52 val comment:String when(age) { 50 -> comment = "Half century" 25 -> comment = "Young" else -> comment = "Hmm" } println(comment) val anotherComment:String when { age > 50 -> anotherComment = "Getting old" else -> anotherComment = "You'll get there" } println(anotherComment)
The when statements can also be written as expressions like this.
//As an expression val finalComment = when { age > 50 -> "Getting Old" else -> "You'll get there" } println(finalComment)
You can find the video here.
Kotlin Video Series – String (Part 2)
This is the fifth part of the video series on Kotlin language.
Read and watch the other parts here.
1. | Hello Kotlin |
2. | Declaring Variables |
3. | Data Types |
4. | String (Part 1) |
In this video, we discuss string comparison and string templates.
String comparison is a breeze in Kotlin. We can use == to compare two string literals.
fun main(args:Array<String>){ var lang1 = "java" var lang2 = "Java" println(lang1 == lang2) }
Strings can be compared using the .compareTo method too, which gives you an option to compare the values ignoring the case, like this.
println(lang1.compareTo(lang2,true) == 0)
String templates allow you to embed values in a string using ${expression}
var comments = "$lang1 is cool" println(comments) comments = "${lang2} is cool" println(comments)
You can find the video here.