The Programmers Guide To Kotlin - The Basics
Written by Mike James   
Monday, 01 August 2022
Article Index
The Programmers Guide To Kotlin - The Basics
Semicolons
Var & Val
Null Safety

Kotlin is a better Java and it is the main way to create apps for Android. What better time could there be to find out what it is and how to use it? 

Programmer's Guide To Kotlin Third Edition

kotlin3e360

You can buy it from: Amazon

Contents

  1. What makes Kotlin Special
  2. The Basics:Variables,Primitive Types and Functions 
  3. Control
         Extract: If and When 
  4. Strings and Arrays
  5. The Class & The Object ***NEW!
  6. Inheritance
  7. The Type Hierarchy
  8. Generics
  9. Collections, Iterators, Sequences & Ranges
        Extract: Iterators & Sequences 
  10. Advanced functions 
  11. Anonymous, Lamdas & Inline Functions
  12. Data classes, enums and destructuring
        Extract: Destructuring 
  13. Exceptions, Annotations & Reflection
  14. Coroutines
        Extract: Coroutines 
  15. Working with Java
        Extract: Using Swing
  16. Compose Multiplatform
        Extract: Compose Layout 

<ASIN:B0D8H4N8SK>

There are some very basic aspects of Kotlin that you will encounter as soon as you start programming. This chapter introduces these in the rough order that you are likely to encounter them when you first try to read a Kotlin example program. As such it is slightly less logically organized and not as complete as subsequent chapters in order to answer your questions as soon as possible. The intent is to give you enough Kotlin to understand most of an example program.

Functions

As previously stated, the Kotlin principle is to make use of good compile-time syntax to generate the same run-time code the equivalent Java would have produced. The first thing that you notice is that Kotlin has functions that just “float about” without being attached to any class or object. This isn't the way Java works, as any functions you write in Java have to be part of a class. In other words, in Java all functions are methods. In Kotlin most functions are methods, but some functions are just functions.

In Kotlin you can define package level functions. For example, the simplest version of a hello world program is:

fun main(args: Array<String>){
 println("Hello World")
}

You can see that we define a function using fun and curly brackets. You can also see that :type is how you define the type of a variable. Kotlin is strongly-typed, but the compiler uses type inference and you can leave out type declarations a lot of the time. That is every variable has an assigned type, but often the compiler can deduce its type without you having to state it explicitly.

Most Java programmers moving to Kotlin don't leave out type declarations because it is what they are used to. Sometimes an explicit type declaration makes the code clearer, and sometimes it just makes it look more complex.

All is simple so far, but how can Kotlin support package level functions and still be compatible with Java and the JVM? Easy, they aren't really naked functions. The compiler translates them to static methods of a Java class called packagename.filename, where filename is the name of the file including the Kt. So, if the hello world function given earlier was stored in a file called Hello.kt, the class is demo.HelloKt assuming the package is called demo. This means that to call main from a Java program you would use:

demo.main();

You can change the name of the default class using an annotation:

@file:JvmName("class name")

This use of a default class is how Kotlin implements all other instances of functions that don't seem to belong to a class.

Once you have seen how to declare a function, you can almost guess the rest. You can specify a return type or the special type Unit if it doesn't return a value.

Does this mean Kotlin supports first class functions? A first class function is variously defined to be a function that is on an equal footing with an object and can be used in the same way. In Kotlin functions are methods of an object, even if this is hidden behind the scenes, so Kotlin functions cannot be true first class functions. However, they can be passed as parameters to other function and in this slightly lesser sense they are first class, albeit subject to some restrictions explained in Chapter 10. There is also the surprising ability to make any object a function which is even more like a first class function and this is explained in Chapter 11.

Functions are something that Kotlin excels in and Chapters 10 and 11 are dedicated to explaining all of their features. However, a quick look at any Kotlin code soon reveals some key points.

You can use a single expression to define a function:

fun sum(a: Int, b: Int): Int = a + b

and, as long as the return type is obvious, you don't need to declare it. You always need to declare it if the function has a body, unless it is Unit.

You can use default and named arguments:

fun sum(a: Int, b: Int=1): Int{
 return a+b
}

You can then call the function using:

sum(1) 

to get the result 2 or use:

sum(a = 1,b = 2)

to get the result 3.

You can’t use named parameters to call Java functions.



Last Updated ( Tuesday, 02 August 2022 )