Programming Kotlin

Book description

Familiarize yourself with all of Kotlin's features with this in-depth guide

About This Book

  • Get a thorough introduction to Kotlin
  • Learn to use Java code alongside Kotlin without any hiccups
  • Get a complete overview of null safety, Generics, and many more interesting features

Who This Book Is For

The book is for existing Java developers who want to learn more about an alternative JVM language. If you want to see what Kotlin has to offer, this book is ideal for you.

What You Will Learn

  • Use new features to write structured and readable object-oriented code
  • Find out how to use lambdas and higher order functions to write clean, reusable, and simple code
  • Write unit tests and integrate Kotlin tests with Java code in a transitioning code base
  • Write real-world production code in Kotlin in the style of microservices
  • Leverage Kotlin's extensions to the Java collections library
  • Use destructuring expressions and find out how to write your own
  • Write code that avoids null pointer errors and see how Java-nullable code can integrate with features in a Kotlin codebase
  • Discover how to write functions in Kotlin, see the new features available, and extend existing libraries
  • Learn to write an algebraic data types and figure out when they should be used

In Detail

Kotlin has been making waves ever since it was open sourced by JetBrains in 2011; it has been praised by developers across the world and is already being adopted by companies. This book provides a detailed introduction to Kotlin that shows you all its features and will enable you to write Kotlin code to production.

We start with the basics: get you familiar with running Kotlin code, setting up, tools, and instructions that you can use to write basic programs. Next, we cover object oriented code: functions, lambdas, and properties - all while using Kotlin's new features.

Then, we move on to null safety aspects and type parameterization. We show you how to destructure expressions and even write your own. We also take you through important topics like testing, concurrency, microservices, and a whole lot more. By the end of this book you will be able to compose different services and build your own applications.

Style and approach

An easy to follow guide that covers the full set of features in Kotlin programming.

Table of contents

  1. Programming Kotlin
    1. Programming Kotlin
    2. Credits
    3. About the Authors
    4. About the Reviewers
    5. www.PacktPub.com
      1. Why subscribe?
    6. Customer Feedback
    7. Preface
      1. What this book covers 
      2. What you need for this book 
      3. Who this book is for 
      4. Conventions 
      5. Reader feedback
      6. Customer support
        1. Downloading the example code 
        2. Downloading the color images of this book 
        3. Errata
        4. Piracy
        5. Questions
    8. 1. Getting Started with Kotlin
      1. Using the command line to compile and run Kotlin code
      2. Kotlin runtime
      3. The REPL
      4. Kotlin for scripting
      5. Kotlin with Gradle
      6. Kotlin with Maven
      7. IntelliJ and Kotlin
      8. Eclipse and Kotlin
      9. Mixing Kotlin and Java in a project
      10. Summary
    9. 2. Kotlin Basics
      1. Vals and vars
      2. Type inference
      3. Basic types
        1. Numbers
        2. Booleans
        3. Chars
        4. Strings
        5. Arrays
      4. Comments
      5. Packages
      6. Imports
        1. Wildcard imports
        2. Import renaming
      7. String templates
      8. Ranges
      9. Loops
      10. Exception handling
      11. Instantiating classes
      12. Referential equality and structural equality
      13. This expression
        1. Scope
      14. Visibility modifiers
        1. Private
        2. Protected
        3. Internal
      15. Control flow as expressions
      16. Null syntax
        1. Smart casts
        2. Explicit casting
      17. When expression
        1. When (value)
          1. When without argument
        2. Function Return
      18. Type hierarchy
      19. Summary
    10. 3. Object-Oriented Programming in Kotlin
      1. Classes
        1. Access levels
        2. Nested classes
        3. Data classes
        4. Enum classes
        5. Static methods and companion objects
      2. Interfaces
      3. Inheritance
      4. Visibility modifiers
      5. Abstract classes
      6. Interface or abstract class
      7. Polymorphism
      8. Overriding rules
      9. Inheritance versus composition
      10. Class delegation
      11. Sealed classes
      12. Summary
    11. 4. Functions in Kotlin
      1. Defining functions
      2. Single expression functions
      3. Member functions
      4. Local functions
      5. Top-level functions
      6. Named parameters
      7. Default parameters
      8. Extension functions
        1. Extension function precedence
        2. Extension functions on nulls
        3. Member extension functions
        4. Overriding member extension functions
        5. Companion object extensions
        6. Multiple return values
        7. Infix functions
      9. Operators
        1. Operator overloading
        2. Basic operators
        3. In/contains
        4. Get/set
          1. Invoke
        5. Comparison
        6. Assignment
        7. Java interop
      10. Function literals
      11. Tail recursive functions
      12. Varargs
        1. Spread operator
      13. Standard library functions
        1. Apply
        2. Let
        3. With
        4. Run
        5. Lazy
        6. Use
        7. Repeat
        8. Require/assert/check
      14. Generic functions
      15. Pure functions
      16. Java from Kotlin
        1. Getters and setters
        2. Single abstract methods
        3. Escaping Kotlin identifiers
        4. Java void methods
      17. Kotlin from Java
        1. Top-level functions
        2. Default parameters
        3. Object and static methods
        4. Erasure naming
        5. Checked exceptions
      18. Summary
    12. 5. Higher Order Functions and Functional Programming
      1. Higher order functions
        1. Returning a function
        2. Function assignment
      2. Closures
      3. Anonymous functions
      4. Function references
        1. Top-level function references
        2. Member and extension function references
        3. Bound references
      5. Function-literal receivers
      6. Functions in the JVM
        1. Bytecode
      7. Function composition
      8. Inline functions
        1. Noinline
      9. Currying and partial application
        1. Currying in action
        2. Adding currying support
      10. Memoization
        1. Implementing memoization
      11. Type alias
      12. Either
        1. Fold
        2. Projection
        3. Further projection functions
      13. Custom DSLs
        1. Infix functions as keywords
        2. Using function receivers in a DSL
      14. Validation and error accumulation
      15. Summary
    13. 6. Properties
      1. Why use properties?
      2. Syntax and variations
      3. Visibility
      4. Late initialization
      5. Delegated properties
      6. Lazy initializations
      7. Lateinit versus lazy
      8. Observables
      9. A non-null property delegate
      10. Properties or methods?
      11. Summary
    14. 7. Null Safety, Reflection, and Annotations
      1. Nullable types
      2. Smart cast
      3. Safe null access
        1. Force operator
      4. Elvis operator
      5. Safe casting
      6. Optionals
        1. Creating and returning an Optional
        2. Using an Optional
      7. Reflection
        1. KClass
        2. Instantiation using reflection
      8. Constructors
        1. Instantiation with callBy
      9. Objects and companions
      10. Useful KClass properties
      11. Reflective functions and properties
        1. Invoking a function reflectively
        2. Declared and undeclared
      12. Annotations
        1. Annotation parameters
      13. Standard annotations
        1. @JvmName
        2. @JvmStatic
        3. @Throws
        4. @JvmOverloads
      14. Runtime annotation discovery
      15. Summary
    15. 8. Generics
      1. Parameterised functions
      2. Parameterized types
      3. Bounded polymorphism
        1. Upper bounds
          1. Multiple bounds
      4. Type variance
        1. Invariance
        2. Covariance
        3. Covariant return
        4. Contravariance
        5. Variance overview
      5. Nothing type
      6. Type projection
      7. Type erasure
      8. Type reification
      9. Recursive type bounds
      10. Algebraic data types
      11. Summary
    16. 9. Data Classes
      1. Automatic creation of getters and setters
      2. The copy method
      3. toString out of the box
      4. hashCode and equals methods generated for you
      5. Destructed declarations
      6. Destructing types
      7. Data class definition rules
      8. Limitations
      9. Summary
    17. 10. Collections
      1. Class hierarchy
      2. Arrays
      3. Lists
      4. Maps
      5. Sets
      6. Read-only views
      7. Indexed access
      8. Sequences
      9. Summary
    18. 11. Testing in Kotlin
      1. Getting started
      2. Choosing a spec
      3. Matchers
        1. String matchers
        2. Collection matchers
        3. Floating point matchers
          1. Expecting exceptions
        4. Combining matchers
        5. Custom matchers
      4. Inspectors
      5. Interceptors
        1. The test case interceptor
        2. The spec interceptor
      6. Project config
      7. Property testing
        1. Specifying a generator
          1. A custom generator
      8. Table-driven testing
        1. Testing non-deterministic code
      9. Tags, conditions, and config
        1. Config
        2. Conditions
        3. Tags
          1. One instance
        4. Resources
      10. Summary
    19. 12. Microservices with Kotlin
      1. Definition
      2. Drawbacks
      3. Why microservices?
      4. Lagom
      5. Defining services
      6. Implementing a Lagom service
      7. Summary
    20. 13. Concurrency
      1. Threads
        1. Blocking
          1. Creating a thread
          2. Stopping a thread
          3. Thread interrupts
          4. CPU-bound versus I/O-bound
      2. Deadlocks and livelocks
      3. Executors
      4. Race conditions
        1. Monitors
        2. Locks
          1. Read-write locks
        3. Semaphores
          1. The bounded buffer problem
        4. Concurrent collections
          1. ConcurrentHashMap
          2. A blocking queue
        5. Atomic variables
        6. CountDownLatch
        7. Cyclic Barrier
        8. Non-blocking I/O and asynchronous programming
        9. Futures
      5. Summary

Product information

  • Title: Programming Kotlin
  • Author(s): Stephen Samuel, Stefan Bocutiu
  • Release date: January 2017
  • Publisher(s): Packt Publishing
  • ISBN: 9781787126367