Chapter 4, Introduction to Kotlin

  1. Yes, variables and expressions can be used inside string literals beginning with a $ sign.
  2. A nullable variable/argument can be assigned null at any point of code. However, before using, defensive code must be used to check for null safety. Kotlin has reduced the chances of NullPointerException using a nullable variable/argument. An example of a nullable variable/argument is var no : Int? = 0, which uses ? at the end of the data type.
  3. Explicit casting in Kotlin is where when a type checking is done using the is a keyword. Within the block of code that follows, there is no need to do a type casting to the destination type. Here is the following code:
var s : Any = "Shazin";
if (s is String) {
// No need to cast s of type Any to type String inside this block of code like java does using ((String) s)
// All properties and functions of String class can be used without type casting
println(s.length);
}
  1. Object expressions and object declarations can be used to create and use objects without creating a class declaration. Object expressions can be used to create objects and assign them to variables, function arguments without a class. Object declarations have a name and can be used to create singleton objects that cannot be assigned to variables or arguments, but whose functions can be called using the name.
  2. A companion object is an object declaration that begins with a companion keyword inside of a class. Functions inside the companion object can be called just as if they are functions of the enclosing class.
  3. Infix functions are a special type of functions that can be used with two operands similar to the 1 + 2 expression. An Infix function can be defined using the infix keyword.
  4. No, variables defined inside a local function are out of scope for enclosing function, but it works the other way round.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.42.134