Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Kotlin (Hands On)

Android Maestro
August 24, 2019
96

Introduction to Kotlin (Hands On)

Android Maestro

August 24, 2019
Tweet

Transcript

  1. Comments Similar to any modern language, Kotlin supports single line

    or multi line block comments. Comments are a good software practice that ensures readability and proper documentation of code.
  2. Variables A variable is a memory location that can be

    referenced and used by a program to temporarily store information, e.g.a calculator program uses variables to store operands. Type refers to the particular kind of data that is held by a variable
  3. Concept of Mutability and Immutability Mutable variables refers to variables

    that can be modified or updated after initialization while immutable objects refer to objects that cannot be changed or modified after initialization.
  4. Kotlin expresseses mutability through var keyword when defining variables and

    immutability using the keyword val. In kotlin variables defined using the keyword var can be reassigned whereas variables defined using val cannot be reassigned Two ways of defining a variable: Explicitly stating the variable type e.g. int Defining Variables in Kotlin
  5. Type Inferencing Kotlin includes a feature called type inference that

    allows you to omit the type definition for variables that are assigned a value when they are declared. This aims at reducing redundancy.
  6. Constants Constants are pieces of information that you want to

    remain absolutely positively immutable and not to change at all. A constant must be declared outside any function because it is as assigned at compile time, while functions are called at run time. • String • Int • Double • Float • Long • Short • Byte • Char • Boolean
  7. String Templates Kotlin features string templates to help include the

    value of a variable inside a string’s quotation marks.
  8. Kotlin Null Safety Kotlin’s type system is aimed at eliminating

    danger of null references from code, also referred to as The Billion Dollar Mistake. Accessing a null reference will result in a Null Pointer Exception Kotlin distinguishes references that can hold null (nullable references) and those that can not (non-null references)
  9. Null Safety Kotlin distinguishes between nullable and non-nullable types. There

    exists a danger of performing an operation on a nullable type e.g. a.length() because it may not exist. Kotlin prevents you from calling functions on a value defined as nullable until you have accepted responsibility for this unsafe situation.
  10. Safe call operator (?) Using the safe call operator (?)

    in your function call, the compiler checks for a null value, if it finds a null value its skips over the call and does not evaluate it.
  11. DOUBLE-BANG OPERATOR(!!) The double-bang operator also referred to as the

    non-null assertion operator can be used to call a function on a nullable type. It is a more drastic option than safe call operator and should not be generally used. If the compiler encounters an operation on a null variable, it throws a null pointer exception.
  12. Lateinit and Lazy Properties declared as having a non-null type

    must be initialized in the constructor, however in some cases this may not be convenient. Marking your variables with the lateinit modifier (late initialization) allows you to define the variables at a later point in the program flow, however a run time error will be thrown if the program attempts to use the variable and it is yet to be initialized.
  13. Lazy Initialization The concept of lazy initialization was designed to

    prevent unnecessary initialization of objects, especially objects that have cumbersome initialization processes.Kotlin language has built in support for lazy initialization. When using lazy property, initialization occurs when the variable is first accessed, subsequent calls return the same instance.
  14. Visibility Modifiers Visibility modifiers are keywords that define the accessibility

    of classes, objects, interfaces, functions, etc. Kotlin has four visibility modifiers; • private means visible inside this class only (including all its members); • protected — same as private + visible in subclasses too; • internal — any client inside this module who sees the declaring class sees its internal members; • public — any client who sees the declaring class sees its public members.
  15. Functions A function is a block of code that performs

    a specific function and may return a value of a specific type. In Kotlin, a function is defined using the keyword fun; it’s fun, right? Parameters Function parameters are declared using pascal notation, i.e. name:type Parameters are separated using commas
  16. Default Arguments Function parameters can have default values, which are

    used when a corresponding argument is omitted.
  17. Named Arguments Function parameters can be named when calling functions,

    this is helpful when a function has very many parameters. Helps in readability of code. (Beautiful Code)
  18. Unit-returning functions A function that does not return any useful

    value is said to have it’s return type as null. A unit return type does not have to be explicitly stated.
  19. Single Expression Functions When a function returns a single expression,

    the curly braces can be omitted and the body is specified after a = symbol:
  20. Control Flow Control flow allows you to describe the conditions

    for when specific portions of your program should run. Includes; if statement, when, loops
  21. If/else statement The program executes the code in the if

    block or curly braces if the condition is true
  22. Ranges The . . operator, i.e. 1 .. 5, signals

    a range. A range includes all values from the left of the .. operator to the value on the right. 1 .. 5 includes 1,2,3,4,5. The in keyword is used to check whether a value is in range.
  23. When Expressions When expressions allows you to write conditions to

    check for and will execute the corresponding code when the condition evaluates as true. When provides a more concise syntax.
  24. Any Kotlin allows us to define variables or even argument

    types using Any(), Any takes in any form of input, one can use an if/else expression or when to perform different operations depending on the data type.
  25. Constructors A class in Kotlin can have a primary constructor

    and one or more secondary constructors. The primary constructor is part of the class header: it goes after the class name (and optional type parameters).
  26. Inheritance All classes in Kotlin have a common superclass Any,

    that is the default superclass for a class with no supertypes declared: Any has three methods: equals(), hashCode() and toString(). Thus, they are defined for all Kotlin classes.
  27. To declare an explicit supertype, place the type after a

    colon in the class header: By default all classes in kotlin are final, keyword open is used to allow a class to be inherited Inheritance
  28. Overriding methods and properties In Kotlin, methods to be overridden

    have to be explicitly marked with the keyword open.
  29. Data Classes (PoJo) We frequently create classes whose main purpose

    is to hold data. In such a class some standard functionality and utility functions are often mechanically derivable from the data. In Kotlin, this is called a data class and is marked as data:
  30. Abstract classes A class and some of its members may

    be declared abstract. An abstract member does not have an implementation in its class. Note that we do not need to annotate an abstract class or function with open – it goes without saying.
  31. Companion Object If you need to write a function that

    can be called without having a class instance but needs access to the internals of a class (for example, a factory method), you can write it as a member of an object declaration inside that class.