Kotlin Understanding Part -2

Richa Sharma
3 min readOct 30, 2022

Hi All, Hope you all are doing great…. In last blog we have started with understanding kotlin concepts and have learned few concept in part -1. If you have not gone through it I would recommend you to please have a look.

So in current part-2 we will go through some more basic concepts as mentioned below:

  1. String Interpolation
  2. Data Class
  3. lateinit
  4. lazy

so, let’s start with String Interpolation in kotlin…

String Interpolation :

  • In terms of kotlin we have some different concepts raegrding String interpolation.
  • String interpolation allows us to concatenate constant strings efficiently and variables to create another string.
  • With some other languages we generally do it with “+”.
for example :
fun main(args: Array<String>){
var name = "Richa"
var greetings = "Hello" + name
print(greetings)
}
  • Now if you are using koltin language so you will receive recommendation in IDE.
  • In kotlin we don’t need “+” instead we can simply use “$” symbol and pass the string variable. This will print the concat string in console.
fun main(args: Array<String>){
var name = "Richa"
var greetings = "Hello$name"
print(greetings)
}
  • Now suppose you want to print number of character in print. You cannot pass it like “Hello$name.$length” .
  • This will give you an error because the length is dependent on string greetings so this we need to use “{}”.
fun main(args: Array<String>){
var name = "Richa"
var greetings = "Hello$name"
print(greetings)
print
("the number of characters are ${name.length}")
}

Data Class

  • By default every class has super class of “Any”.
  • This Any class has few by default functions those are :
  1. equals(): Boolean
  2. hasCode(): Int
  3. toString(): String
  4. Kotlin provide copy() too.
for example:
data class Employee(var name: String, var id: Int)
  • The purpose of data class to deliver the data not the object , if we compare 2 object of this class with same values it will be equal where as if it was normal class it would not be equal because object reference are different.
  • Note : In data classes the primary constructor should contain parameters as property i.e var or val.

lateinit

  • The lateinit keyword is used for late initialization of variables.
private lateinit var name: String
// demo function to get employee name using the empId
fun fetchEmpName(empId: String) {
name = repository.getEmpName(empId)
}
  • We should be very sure that your lateinit variable will be initialized before accessing it otherwise we will get
    UninitializedPropertyAccessException”.
  • Lateinit is allowed for non-primitive data types only and the variable can’t be of null type and we should always use var not val.

Lazy

  • Lazy keyord is used in some certain classes whose object initialization is very heavy and so much time taking that it results in the delay of the whole class creation process.
  • This can be happen for example if we are working in a project with big team which has multiple classes.
  • Due to heavy class the object creation will be slow so here we can use feature lazy keyword.
class EmpClass {
private val resultObject: ResultClass by lazy {
ResultClass()
}
}
  • now here Resultclass is the heavy class with help of lazy keyword the object will be created only when it is called until than it will not be created.

In next blog we will go through some few more concepts like scope function, extension, higher order function.

Happy Reading :)

source: Google

--

--