Instruction 1

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Declared and Inferred Kotlin Types

During program execution, a variable could be an Int or a String. Kotlin is a statically typed language. This means the type of a variable or object remains fixed throughout the duration of a program.

val amount = 200
fun main() {
  val amount = 200
  println(amount::class.simpleName)
}
Int
fun main() {
  val amount = 200f
  println(amount::class.simpleName)
}
Float
fun main() {
  val amount = 200.0
  println(amount::class.simpleName)
}
Double
fun main() {
  val amount: Float = 200f
  println(amount::class.simpleName)
}
Float
fun main() {
  val amount: Double = 200f
  println(amount::class.simpleName)
}
The floating-point literal does not conform to the expected type Double
fun main() {
  val name = "Bob"
  println(name::class.simpleName) // Prints the type or class representing the type for the object
}
String
fun main() {
  val name: String = "Bob"
  println(name::class.simpleName) // Prints the type or class representing the type for the object
}
String

Checking Types

There could be times when you’re unsure of the type of data you’re working with. Kotlin has a type called Any from which all other classes inherit. This means Any is the parent of every non-nullable object in Kotlin. If, at any point in your code, you have an Any type, you can use the is operator to check if an object is some other type. This expression will result in a Boolean - either true or false.

fun main() {
  val name: Any = "Bob"
  val isString = name is String
  println("name is String: " + isString)
}
name is String: true
fun main() {
  val name: Any = "Bob"
  val isString = name::class.simpleName == "String"
  println("name is String: " + isString)
}

Using Smart Casts

Kotlin is a programming language that helps you write concise and clear code. It has a feature called smart cast. Smart cast allows you to cast a variable to a different type. If the conversion succeeds, execution continues with the variable. This means you don’t have to be verbose while writing code and can achieve more with less. Once converted, you can use any functions and operators available for that type. This feature is especially useful when you are working with variables of an unknown type.

fun main() {
  val input: Any = "Bob"
  if (input is String){
    print(input.uppercase()) // Calling String methods on the variable
  }
}
BOB
fun main() {
  val input: Any = 10
  if (input is Int){
    print(input.div(2)) // Calling Int methods on the variable
  }
}
Smart cast to 'String' is impossible, because 'name' is a local variable that is captured by a changing closure
Smart cast to 'String' is impossible, because 'name' is a mutable property that could have been changed by this time

Cast Operator

In the previous section, you used the is operator to check for a specific type. If the variable was the inquired type, Kotlin’s smart cast feature confirms its safe use. You can use the variable as expected. Kotlin did the declaration conversion (casting) for you. You can also do this casting yourself, but this cast uses the unsafe cast operator as.

fun main() {
  val input: Any = 2
  println(input::class.simpleName) // Kotlin intelligently treats the input as an Int even though it's explicitly declared as an Any

  val inputCast = input as Int
  println(inputCast::class.simpleName) // You manually cast the variable as an Int

  println(inputCast.minus(2)) // You call an Int method on the variable
}
fun main() {
  val input: Any = "2"
  println(input::class.simpleName) // Kotlin intelligently treats the input as a String even though it's explicitly declared as an Any

  val inputCast = input as Int
  println(inputCast::class.simpleName) // You manually cast the variable as an Int when it's a String

  println(inputCast.minus(2)) // You call an Int method on the variable
}
String
Exception in thread "main" java.lang.ClassCastException: class java.lang.String cannot be cast to class java.lang.Integer (java.lang.String and java.lang.Integer are in module java.base of loader 'bootstrap')
at FileKt.main (File.kt:10)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (:-2)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (:-1)
fun main() {
  val input: Any = "2"
  println(input::class.simpleName) // Kotlin intelligently treats the input as a String even though it's explicitly declared as an Any

  val inputCast = input as? Int
  //println(inputCast::class.simpleName) // You manually cast the variable as an Int when it's a String

  println(inputCast?.minus(2)) // You call an Int method on the variable
}
String
null
See forum comments
Download course materials from Github
Previous: Introduction Next: Instruction 2