Kotlin Basics

📌Define variables (val vs var)

  • val is immutable (read-only) and we can only assign a value to them exactly one time.
  • var is mutable and can be reassigned.

📌Types in Kotlin

  • Simple reference uses $
  • Complex references uses ${}
  • Raw Strings ”””

📌Control flow: if, when, for, while

  • if — in Kotlin if is an expression, so it returns a value. There is no ternary operator.
  • when — replaces “switch” from Java. We can also check a value for being or not in a specific range and we can also check if a variable is or not of a particular type.
  • for — iterates through anything that provides an iterator. Can use the withIndex library function.
  • while and do … while — same behavior like in Java.
if/else
when
when with range
when with check type
for
for with index
while and do…while

📌Equality checks

  • In Kotlin we have structural equality (a check for equals()) ==
  • Referential equality (two references point to the same object) ===

📌Null safety

  • In an effort to rid the world of NullPointerException, variable types in Kotlin don’t allow the assignment of null. 👼👼👼
  • In order to use a variable that can be null, declare it nullable by adding ? at the end of its type.
  • An explicit call to throw NullPointerException()
  • Usage of the !! operator (not-null assertion operator)
  • Some data inconsistency with regard to initialization
  • Java inter-operation

--

--

Engineering Manager @Adobe | Android @GoogleDevExpert | Blogger, Speaker, Trainer #android #kotlin #gde #agile #leadership

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store