Kotlin Programming Cookbook

Book description

Discover Android programming and web development by understanding the concepts of Kotlin Programming

About This Book

  • Practical solutions to your common programming problems with Kotlin 1.1
  • Leverage the functional power of Kotlin to ease your Android application development
  • Learn to use Java code in conjunction with Kotlin

Who This Book Is For

This book will appeal to Kotlin developers keen to find solutions for their common programming problems. Java programming knowledge would be an added advantage.

What You Will Learn

  • Understand the basics and object-oriented concepts of Kotlin Programming
  • Explore the full potential of collection frameworks in Kotlin
  • Work with SQLite databases in Android, make network calls, and fetch data over a network
  • Use Kotlin's Anko library for efficient and quick Android development
  • Uncover some of the best features of Kotlin: Lambdas and Delegates
  • Set up web service development environments, write servlets, and build RESTful services with Kotlin
  • Learn how to write unit tests, integration tests, and instrumentation/acceptance tests.

In Detail

The Android team has announced first-class support for Kotlin 1.1. This acts as an added boost to the language and more and more developers are now looking at Kotlin for their application development. This recipe-based book will be your guide to learning the Kotlin programming language.

The recipes in this book build from simple language concepts to more complex applications of the language. After the fundamentals of the language, you will learn how to apply the object-oriented programming features of Kotlin 1.1. Programming with Lambdas will show you how to use the functional power of Kotlin.

This book has recipes that will get you started with Android programming with Kotlin 1.1, providing quick solutions to common problems encountered during Android app development. You will also be taken through recipes that will teach you microservice and concurrent programming with Kotlin. Going forward, you will learn to test and secure your applications with Kotlin. Finally, this book supplies recipes that will help you migrate your Java code to Kotlin and will help ensure that it's interoperable with Java.

Style and approach

This book explains concepts related to Kotlin Programming using a practical approach and with the help of easy-to-follow recipes.

Publisher resources

Download Example Code

Table of contents

  1. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Conventions used
    4. Sections
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    5. Get in touch
      1. Reviews
  2. Installation and Working with Environment
    1. Introduction
    2. Creating Kotlin Android project
      1. Getting ready
      2. How to do it...
      3. There's more...
    3. How to use Gradle to run Kotlin code
      1. Getting ready
      2. How to do it...
      3. There's more...
      4. See also
    4. How to run a Kotlin compiled class
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    5. How to build a self-executable JAR with Gradle and Kotlin
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Reading console input in Kotlin
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Converting Java code to Kotlin and vice versa
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. How to write an idiomatic logger in Kotlin
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    9. Escaping for Java identifiers that are keywords in Kotlin
      1. Getting ready
      2. How to do it...
      3. How it works...
    10. Disambiguating using the "as" keyword to locally rename the clashing entity
      1. Getting ready
      2. How to do it...
    11. Doing bit manipulations in Kotlin
      1. Getting ready
      2. How to do it...
        1. Or
        2. and
        3. xor
        4. inv
        5. shl
        6. shr
        7. ushr
      3. How it works...
    12. Parsing String to Long, Double, or Int
      1. Getting ready
      2. How to do it...
        1. Converting String to Long
        2. Converting string to Long using string.toLongOrNull()
          1. Converting with special radix
        3. Parsing string to Long with special radix
      3. How it works...
      4. There's more...
    13. Using String templates in Kotlin
      1. How to do it...
      2. How it works...
      3. There’s more...
  3. Control Flow
    1. Introduction
    2. Assigning result to an expression using the if keyword
      1. Getting ready
      2. How to do it...
      3. There's more...
    3. Using range with the when expression
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    4. Using when with custom objects
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Using try–catch as an expression
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    6. How to write a swap function in Kotlin using the also function
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    7. How to throw a custom exception in Kotlin
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. How to make a multiconditional loop in Kotlin
      1. Getting ready
      2. How to do it...
      3. How it works...
  4. Classes and Objects
    1. Introduction
    2. Initializing body of constructor
      1. Getting ready
      2. How to do it...
    3. Converting one data type into another
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. How to type check an object
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    5. How to work with an abstract class in Kotlin
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. How to iterate over a class's properties in Kotlin
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    7. How to work with inline properties
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. How to work with nested class
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    9. Getting class in Kotlin
      1. How to do it...
      2. There's more...
    10. Working with delegated properties
      1. Getting ready
      2. How to do it...
      3. There's more...
    11. Working with enums
      1. Getting ready
      2. How to do it...
      3. There's more...
  5. Functions
    1. Introduction
    2. Specifying default values in functions
      1. Getting ready
      2. How to do it...
      3. There's more...
    3. Using named arguments in functions
      1. Getting ready
      2. How to do it...
    4. Creating the RecyclerView Adapter in Kotlin
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    5. Creating getter setters in Kotlin
      1. Getting ready
      2. How to do it...
      3. There's more...
    6. Passing variable arguments to a function
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    7. Passing a function as a parameter to another
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Declaring a static function
      1. Getting ready
      2. How to do it...
    9. Using the use keyword in Kotlin
      1. Getting ready
      2. How to do it...
      3. How it works...
    10. Working with closures
      1. Getting ready
      2. How to do it...
      3. There's more...
    11. Function literals with receivers
      1. Getting ready
      2. How to do it...
    12. Working with anonymous functions
      1. Getting ready
      2. How to do it...
  6. Object-Oriented Programming
    1. Introduction
    2. Working with interfaces in Kotlin
      1. Getting ready
      2. How to do it…
      3. There's more…
    3. How to implement complicated interfaces with multiple overridden methods in Kotlin
      1. Getting ready
      2. How to do it…
    4. How to extend a class in Kotlin (Inheritance and Extension functions)
      1. Getting ready
      2. How to do it…
        1. Extension functions
      3. How it works…
      4. There's more…
    5. How to work with Generics in Kotlin
      1. Getting ready
      2. How to do it…
      3. There's more…
    6. How to implement polymorphism in Kotlin
      1. Getting ready
      2. How to do it…
    7. Restricting class hierarchies
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
  7. Collections Framework
    1. Introduction
    2. How to merge two collections
      1. Getting ready
      2. How to do it…
    3. Splitting original collection into pair of collections
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    4. Sorting a list by specified comparator
      1. Getting ready
      2. How to do it…
      3. How it works…
    5. Sorting in descending order
      1. Getting ready
      2. How to do it…
      3. How it works…
    6. Parsing a JSON response using Gson
      1. Getting ready
      2. How to do it…
      3. There's more…
    7. How to filter and map using lambda expressions
      1. Getting ready
      2. How to do it…
    8. How to sort a list of objects and keep null objects at the end
      1. Getting ready
      2. How to do it…
      3. How it works…
    9. How to implement a lazy list in Kotlin
      1. Getting ready
      2. How to do it…
      3. How it works…
    10. How to pad a string in Kotlin
      1. Getting ready
      2. How to do it…
      3. How it works…
    11. How to flatten an array or map
      1. Getting ready
      2. How to do it…
      3. How it works…
    12. How to sort collection by multiple fields in Kotlin
      1. Getting ready
      2. How to do it…
      3. How it works…
    13. How to use limit in Kotlin list
      1. Getting ready
      2. How to do it…
    14. How to create a 2D array in Kotlin
      1. Getting ready
      2. How to do it…
      3. There's more…
    15. How to skip the first "n" entries in Kotlin
      1. Getting ready
      2. How to do it…
      3. How it works…
  8. Handling File Operations in Kotlin
    1. Introduction
    2. Reading from files using InputReader
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    3. Reading all lines in a file using InputReader
      1. Getting ready
      2. How to do it…
      3. How it works…
    4. Reading line by line using InputReader
      1. Getting ready
      2. How to do it…
      3. How it works…
    5. Reading from files using BufferedReader
      1. Getting ready
      2. How to do it…
      3. How it works…
    6. Reading all lines in a file using BufferedReader
      1. Getting ready
      2. How to do it…
      3. How it works…
    7. Reading line by line using bufferedReader
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    8. Reading string and JSON over network
      1. Getting ready
      2. How to do it…
      3. How it works…
  9. Anko Commons and Extension Function
    1. Introduction
    2. Setting up Anko with Gradle
      1. Getting ready
      2. How to do it…
    3. Extending Android framework using extension function
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    4. Using extensions as properties
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    5. Using intents with Anko
      1. Getting ready
      2. How to do it…
      3. How it works…
    6. Making a call intent using Anko
      1. Getting ready
      2. How to do it…
      3. How it works…
    7. Sending a text intent using Anko
      1. Getting ready
      2. How to do it…
      3. How it works…
    8. Browsing the web browser using Anko
      1. Getting ready
      2. How to do it…
      3. How it works…
    9. Sharing some text using intents in Anko
      1. Getting ready
      2. How to do it…
      3. How it works…
    10. Sending an email using Anko
      1. Getting ready
      2. How to do it…
      3. How it works…
    11. Creating Android dialogs with Anko
      1. Getting ready
      2. How to do it…
    12. Showing an alert dialog with a list of text items
      1. Getting ready
      2. How to do it…
      3. How it works…
    13. Using Anko in Views
      1. Getting ready
      2. How to do it…
    14. Logging using Anko
      1. Getting ready
      2. How to do it…
    15. Handling dimensions with Anko
      1. Getting ready
      2. How to do it…
    16. Version checking on Android
      1. Getting ready
      2. How to do it…
      3. How it works…
  10. Anko Layouts
    1. Introduction
    2. Setting up Anko library for Anko layouts in Gradle
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    3. Creating user-interface programmatically
      1. Getting ready
      2. How to do it…
      3. How it works…
    4. Working with the old code of XML layouts
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    5. Using the provided AnkoComponent interface
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    6. Setting theme for Android views in Anko
      1. Getting ready
      2. How to do it…
    7. Setting layout parameters for Anko views
      1. Getting ready
      2. How to do it…
      3. How it works…
    8. Adding listeners to Anko views
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    9. Inserting XML layouts into DSL
      1. Getting ready
      2. How to do it…
    10. Converting XML files into DSL
      1. Getting ready
      2. How to do it…
    11. Showing Snackbar
      1. Getting ready
      2. How to do it…
    12. Showing Toasts
      1. Getting started
      2. How to do it…
    13. Accessing views using synthetic properties
      1. Getting ready
      2. How to do it…
    14. Accessing views of view groups using extension functions
      1. Getting ready
      2. How to do it…
      3. How it works...
  11. Databases and Dependency Injection
    1. Introduction
    2. Using SQLite database in Kotlin
      1. Getting ready
      2. How to do it…
    3. Creating database tables
      1. Getting ready
      2. How to do it…
    4. Injecting dependencies in Kotlin
      1. Getting ready
      2. How to do it…
      3. How it works…
    5. Reading data from database
      1. Getting ready
      2. How to do it…
    6. Converting database cursor into list of objects
      1. Getting ready
      2. How to do it…
    7. Using parseOpt for nullable object
      1. Getting ready
      2. How to do it…
    8. Inserting data into database
      1. Getting ready
      2. How to do it…
    9. Creating singletons in Kotlin
      1. Getting ready
      2. How to do it…
      3. How it works…
    10. Using Dagger 2 with Kotlin
      1. Getting ready
      2. How to do it…
    11. Using Butterknife with Kotlin
      1. Getting ready
      2. How to do it…
      3. There's more…
  12. Networking and Concurrency
    1. Introduction
    2. How to fetch data over network
      1. Getting ready
      2. How to do it…
      3. There's more…
    3. How to create data class in Kotlin
      1. Getting ready
      2. How to do it…
      3. There's more…
    4. How to copy data class with modifications
      1. Getting ready
      2. How to do it…
      3. There's more…
    5. How to parse JSON data from network to data class
      1. Getting ready
      2. How to do it…
    6. How to download a file in Kotlin
      1. Getting ready
      2. How to do it…
    7. How to use RxJava and Retrofit with Kotlin
      1. Getting ready
      2. How to do it…
    8. How to make an endless list using RecyclerView
      1. Getting ready
      2. How to do it…
      3. How it works…
    9. How to use Anko to run background tasks with Kotlin in Android
      1. Getting ready
      2. How to do it…
      3. How it works…
    10. How to use coroutines to achieve multithreading
      1. Getting ready
      2. How to do it…
      3. There's more…
  13. Lambdas and Delegates
    1. Introduction
    2. Click listeners using lambdas
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    3. Using lazy delegate in Kotlin
      1. Getting ready
      2. How to do it…
      3. How it works…
    4. Using the observable delegate
      1. Getting ready
      2. How to do it…
      3. There's more…
    5. Using vetoable delegate
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    6. Writing your own delegates
      1. Getting ready
      2. How to do it…
      3. How it works…
    7. Using the lateinit modifier
      1. Getting ready
      2. How to do it…
      3. There's more…
    8. Working with SharedPreferences
      1. Getting ready
      2. How to do it…
      3. There's more…
    9. Creating a chain of multiple lets in Kotlin
      1. Getting ready
      2. How to do it…
    10. Creating global variables
      1. Getting ready
      2. How to do it…
  14. Testing
    1. Introduction
    2. Unit testing Kotlin code
      1. Getting ready
      2. How to do it…
      3. How it works…
    3. Unit testing with Mockito
      1. Getting ready
      2. How to do it…
      3. There's more…
        1. Spy object
        2. Mockito limitations
    4. Running instrumentation tests
      1. Getting ready
      2. How to do it…
      3. There's more…
    5. Writing JUnit rules in Kotlin (@Rule)
      1. Getting ready
      2. How to do it…
      3. How it works…
    6. Acceptance tests using Espresso Kotlin
      1. Getting ready
      2. How to do it…
      3. There's more…
    7. Writing assertEquals in Kotlin
      1. Getting ready
      2. How to do it…
  15. Web Services with Kotlin
    1. Introduction
    2. How to run the application on Tomcat
      1. How to do it…
    3. Setting up dependencies for building RESTful services
      1. Getting ready
      2. How to do it…
    4. How to create a REST controller
      1. Getting ready
      2. How to do it…
      3. There's more…
    5. Creating the Application class for Spring Boot
      1. Getting ready
      2. How to do it…
  16. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Kotlin Programming Cookbook
  • Author(s): Aanand Shekhar Roy, Rashi Karanpuria
  • Release date: January 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788472142