Kotlin in Action
Summary

Kotlin in Action guides experienced Java developers from the language basics of Kotlin all the way through building applications to run on the JVM and Android devices. Foreword by Andrey Breslav, Lead Designer of Kotlin.

Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications.

About the Technology

Developers want to get work done - and the less hassle, the better. Coding with Kotlin means less hassle. The Kotlin programming language offers an expressive syntax, a strong intuitive type system, and great tooling support along with seamless interoperability with existing Java code, libraries, and frameworks. Kotlin can be compiled to Java bytecode, so you can use it everywhere Java is used, including Android. And with an effi cient compiler and a small standard library, Kotlin imposes virtually no runtime overhead.

About the Book

Kotlin in Action teaches you to use the Kotlin language for production-quality applications. Written for experienced Java developers, this example-rich book goes further than most language books, covering interesting topics like building DSLs with natural language syntax. The authors are core Kotlin developers, so you can trust that even the gnarly details are dead accurate.

What's Inside

  • Functional programming on the JVM
  • Writing clean and idiomatic code
  • Combining Kotlin and Java
  • Domain-specific languages

About the Reader

This book is for experienced Java developers.

About the Author

Dmitry Jemerov and Svetlana Isakova are core Kotlin developers at JetBrains.

Table of Contents

    PART 1 - INTRODUCING KOTLIN
  1. Kotlin: what and why
  2. Kotlin basics
  3. Defining and calling functions
  4. Classes, objects, and interfaces
  5. Programming with lambdas
  6. The Kotlin type system
  7. PART 2 - EMBRACING KOTLIN
  8. Operator overloading and other conventions
  9. Higher-order functions: lambdas as parameters and return values
  10. Generics
  11. Annotations and reflection
  12. DSL construction
1123843196
Kotlin in Action
Summary

Kotlin in Action guides experienced Java developers from the language basics of Kotlin all the way through building applications to run on the JVM and Android devices. Foreword by Andrey Breslav, Lead Designer of Kotlin.

Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications.

About the Technology

Developers want to get work done - and the less hassle, the better. Coding with Kotlin means less hassle. The Kotlin programming language offers an expressive syntax, a strong intuitive type system, and great tooling support along with seamless interoperability with existing Java code, libraries, and frameworks. Kotlin can be compiled to Java bytecode, so you can use it everywhere Java is used, including Android. And with an effi cient compiler and a small standard library, Kotlin imposes virtually no runtime overhead.

About the Book

Kotlin in Action teaches you to use the Kotlin language for production-quality applications. Written for experienced Java developers, this example-rich book goes further than most language books, covering interesting topics like building DSLs with natural language syntax. The authors are core Kotlin developers, so you can trust that even the gnarly details are dead accurate.

What's Inside

  • Functional programming on the JVM
  • Writing clean and idiomatic code
  • Combining Kotlin and Java
  • Domain-specific languages

About the Reader

This book is for experienced Java developers.

About the Author

Dmitry Jemerov and Svetlana Isakova are core Kotlin developers at JetBrains.

Table of Contents

    PART 1 - INTRODUCING KOTLIN
  1. Kotlin: what and why
  2. Kotlin basics
  3. Defining and calling functions
  4. Classes, objects, and interfaces
  5. Programming with lambdas
  6. The Kotlin type system
  7. PART 2 - EMBRACING KOTLIN
  8. Operator overloading and other conventions
  9. Higher-order functions: lambdas as parameters and return values
  10. Generics
  11. Annotations and reflection
  12. DSL construction
44.99 Out Of Stock
Kotlin in Action

Kotlin in Action

Kotlin in Action

Kotlin in Action

Paperback(1st Edition)

$44.99 
  • SHIP THIS ITEM
    Temporarily Out of Stock Online
  • PICK UP IN STORE

    Your local store may have stock of this item.

Related collections and offers


Overview

Summary

Kotlin in Action guides experienced Java developers from the language basics of Kotlin all the way through building applications to run on the JVM and Android devices. Foreword by Andrey Breslav, Lead Designer of Kotlin.

Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications.

About the Technology

Developers want to get work done - and the less hassle, the better. Coding with Kotlin means less hassle. The Kotlin programming language offers an expressive syntax, a strong intuitive type system, and great tooling support along with seamless interoperability with existing Java code, libraries, and frameworks. Kotlin can be compiled to Java bytecode, so you can use it everywhere Java is used, including Android. And with an effi cient compiler and a small standard library, Kotlin imposes virtually no runtime overhead.

About the Book

Kotlin in Action teaches you to use the Kotlin language for production-quality applications. Written for experienced Java developers, this example-rich book goes further than most language books, covering interesting topics like building DSLs with natural language syntax. The authors are core Kotlin developers, so you can trust that even the gnarly details are dead accurate.

What's Inside

  • Functional programming on the JVM
  • Writing clean and idiomatic code
  • Combining Kotlin and Java
  • Domain-specific languages

About the Reader

This book is for experienced Java developers.

About the Author

Dmitry Jemerov and Svetlana Isakova are core Kotlin developers at JetBrains.

Table of Contents

    PART 1 - INTRODUCING KOTLIN
  1. Kotlin: what and why
  2. Kotlin basics
  3. Defining and calling functions
  4. Classes, objects, and interfaces
  5. Programming with lambdas
  6. The Kotlin type system
  7. PART 2 - EMBRACING KOTLIN
  8. Operator overloading and other conventions
  9. Higher-order functions: lambdas as parameters and return values
  10. Generics
  11. Annotations and reflection
  12. DSL construction

Product Details

ISBN-13: 9781617293290
Publisher: Manning
Publication date: 02/19/2017
Edition description: 1st Edition
Pages: 360
Product dimensions: 7.30(w) x 9.20(h) x 0.90(d)

About the Author

Dmitry Jemerov has been working with JetBrains since 2003. He was one of the initial developers working on Kotlin as the project was started, and he’s deeply familiar with the design of the language and the reasons for the decisions that were made during its development. He’s currently responsible for Kotlin documentation.

Svetlana Isakova began as a member of the Kotlin compiler team and is now a developer advocate for JetBrains. She teaches Kotlin and speaks at conferences worldwide. She is a co-creator of the course "Kotlin for Java developers" at Coursera and is a co-author of the book "Atomic Kotlin."

Table of Contents

Foreword xv

Preface xvii

Acknowledgments xix

About this book xxi

About the authors xxiv

About the cover illustration xxv

Part 1 Introduction Kotlin 1

1 Kotlin: what and why 3

1.1 A taste of Kotlin 3

1.2 Kotlin's primary traits 4

Target platforms: server-side, Android, anywhere Java runs 4

Statically typed 5

Functional and object-oriented 6

Free and open source 7

1.3 Kotlin applications 7

Kotlin on the server side 8

Kotlin on Android 9

1.4 The philosophy of Kotlin 10

Pragmatic 10

Concise 11

Safe 12

Interoperable 12

1.5 Using the Kotlin tools 13

Compiling Kotlin code 13

Plug-in for Intelli IDEA and Android Studio 14

Interactive shell 15

Eclipse plug-in 15

Online playground 15

Java-to-Kotlin converter 15

1.6 Summary 15

2 Kotlin basics 17

2.1 Basic elements: functions and variables 18

Hello, world! 18

Functions 18

Variables 20

Easier string formatting: string templates 22

2.2 Classes and properties 23

Properties 23

Custom accessors 25

Kotlin source code layout: directories and packages 26

2.3 Representing and handling choices: enums and "when" 28

Declaring enum classes 28

Using "when" to deal with enum classes 29

Using "when" with arbitrary objects 30

Using "when" without an argument 31

Smart casts: combining type checks and casts 31

Refactoring: replacing "if with "when" 33

Blocks as branches of "if" and "when" 34

2.4 Iterating over things: "while" and "for" loops 35

The "while" loop 35

Iterating over numbers: ranges and progressions 36

Iterating over maps 37

Using "in" to check collection and range membership 38

2.5 Exceptions in Kotlin 39

"try", "catch", and "finally" 40

"try" as an expression 41

2.6 Summary 42

3 Defining and calling functions 44

3.1 Creating collections in Kotlin 45

3.2 Making functions easier to call 46

Named arguments 47

Default parameter values 48

Getting rid of static utility classes: top-level functions and properties 49

3.3 Adding methods to other people's classes: extension functions and properties 51

Imports and extension functions 53

Calling extension functions from Java 53

Utility functions as extensions 54

No overriding for extension functions 55

Extension properties 56

3.4 Working with collections: varargs, infix calls, and library support 57

Extending the Java Collections API 57

Varargs: functions that accept an arbitrary number of arguments 58

Working with pairs: infix calls and destructuring declarations 59

3.5 Working with strings and regular expressions 60

Splitting strings 60

Regular expressions and triple-quoted strings 61

Multiline triple-quoted strings 62

3.6 Making your code tidy: local functions and extensions 64

3.7 Summary 66

4 Classes, objects, and interfaces 67

4.1 Defining class hierarchies 68

Interfaces in Kotlin 68

Open, final, and abstract modifiers: final by default 70

Visibility modifiers: public by default 73

Inner and nested classes: nested by default 75

Sealed classes: defining restricted class hierarchies 77

4.2 Declaring a class with nontrivial constructors or properties 78

Initializing classes: primary constructor and initializer blocks 79

Secondary constructors: initializing the superclass in different ways 81

Implementing properties declared in interfaces 83

Accessing a backing field from a getter or setter 85

Changing accessor visibility 86

4.3 Compiler-generated methods: data classes and class delegation 87

Universal object methods 87

Data classes: autogenerated implementations of universal methods 89

Class delegation: using the "by" keyword 91

4.4 The "object" keyword: declaring a class and creating an instance, combined 93

Object declarations: singletons made easy 93

Companion objects: a place for factory methods and static members 96

Companion objects as regular objects 98

Object expressions: anonymous inner classes rephrased 100

4.5 Summary 101

5 Programming with lambdas 103

5.1 Lambda expressions and member references 104

Introduction to lambdas: blocks of code as function parameters 104

Lambdas and collections 105

Syntax for lambda expressions 106

Accessing variables in scope 109

Member references 111

5.2 Functional APIs for collections 113

Essentials: filler and map 113

"All", "any", "count", and "find": applying a predicate to a collection 115

GroupBy: converting a list to a map of groups 117

FlalMap and flatten: processing elements in nested collections 117

5.3 Lazy collection operations: sequences 118

Executing sequence operations: intermediate and terminal operations 120

Creating sequences 122

5.4 Using Java functional interfaces 123

Passing a lambda as a parameter to a Java method 124

SAM constructors: explicit conversion of lambdas to functional interfaces 126

5.5 Lambdas with receivers: "with" and "apply" 128

The "with" function 128

The "apply" function 130

5.6 Summary 131

6 The Kotlin type system 133

6.1 Nullability 133

Nullable types 134

The meaning of types 136

Safe call operator: "?." 137

Elvis operator: "?:" 139

Safe casts: "as?" 140

Not-null assertions: "!!" 141

The "let" function 143

Late-initialized properties 145

Extensions for nullable types 146

Nullability of type parameters 148

Nullability and Java 149

6.2 Primitive and other basic types 153

Primitive types: Int, Boolean, and more 153

Nullable primitive types: Int?, Boolean?, and more 154

Number conversions 155

"Any" and "Any?": the root types 157

The Unit type: Kotlin's "void" 157

The Nothing type: "This function never returns" 158

6.3 Collections and arrays 159

Nullability and collections 159

Read-only and mutable collections 161

Kotlin collections and Java 163

Collections as platform types 165

Arrays of objects and primitive types 167

6.4 Summary 170

Part 2 Embracing Kotlin 171

7 Operator overloading and other conventions 173

7.1 Overloading arithmetic operators 174

Overloading binary arithmetic operations 174

Overloading compound assignment operators 177

Overloading unary operators 178

7.2 Overloading comparison operators 180

Equality operators: "equals" 180

Ordering operators: compareTo 181

7.3 Conventions used for collections and ranges 182

Accessing elements by index: "get" and "set" 182

The "in" convention 184

The rangeTo convention 185

The "iterator" convention for the "for" loop 186

7.4 Destructuring declarations and component functions 187

Destructuring declarations and loops 188

7.5 Reusing property accessor logic: delegated properties 189

Delegated properties: the basics 189

Using delegated properties: lazy initialization and "by lazy()" 190

Implementing delegated properties 192

Delegated-property translation rules 195

Storing property values in a map 196

Delegated properties in frameworks 197

7.6 Summary 199

8 Higher-order functions: lambdas as parameters and return values 200

8.1 Declaring higher-order functions 201

Function types 201

Calling functions passed as arguments 202

Using function types from Java 204

Default and null values for parameters with function types 205

Returning functions from functions 207

Removing duplication through lambdas 209

8.2 Inline functions: removing the overhead of lambdas 211

How inlining works 211

Restrictions on inline functions 213

Inlining collection operations 214

Deciding when to declare functions as inline 215

Using inlined lambdas for resource management 216

8.3 Control flow in higher-order functions 217

Return statements in lambdas: return from an enclosing function 217

Returning from lambdas: return with a label 218

Anonymous functions: local returns by default 220

8.4 Summary 221

9 Generics 223

9.1 Generic type parameters 224

Generic functions and properties 224

Declaring generic classes 226

Type parameter constraints 227

Making type parameters non-null 229

9.2 Generics at runtime: erased and reified type parameters 230

Generics at runtime: type checks and casts 230

Declaring functions with reified type parameters 233

Replacing class references with reified type, parameters 235

Restrictions on reified type parameters 236

9.3 Variance: generics and subtyping 237

Why variance exists: passing an argument to a function 237

Classes, types, and subtypes 238

Covariance: preserved subtyping relation 240

Contravariance: reversed subtyping relation 244

Use-site variance: specifying variance for type occurrences 246

Star projection: using * instead of a type argument 248

9.4 Summary 252

10 Annotations and reflection 254

10.1 Declaring and applying annotations 255

Applying annotations 255

Annotation targets 256

Using annotations to customize JSON serialization 258

Declaring annotations 260

Meta-annotations: controlling how an annotation is processed 261

Classes as annotation parameters 262

Generic classes as annotation parameters 263

10.2 Reflection: introspecting Kotlin objects at runtime 264

The Kotlin reflection API: KClass, KCallable, KFunction, and KProperty 265

Implementing object serialization using reflection 268

Customizing serialization with annotations 270

JSON parsing and object deserialization 273

Final deserialization step: callBy() and creating objects using reflection 277

10.3 Summary 281

11 DSL construction 282

11.1 From APIs to DSLs 283

The concept of domain-specific languages 284

Internal DSLs 285

Structure of DSLs 286

Building HTML with an internal DSL 287

11.2 Building structured APIs: lambdas with receivers in DSLs 288

Lambdas with receivers and extension function types 288

Using lambdas with receivers in HTML builders 292

Kotlin builders: enabling abstraction and reuse 296

11.3 More flexible block nesting with the "invoke" convention 299

The "invoke" convention: objects callable as functions 299

The "invoke" convention and functional types 300

The "invoke" convention in DSLs: declaring dependencies in Cradle 301

11.4 Kotlin DSLs in practice 303

Chaining infix calls: "should" in test frameworks 303

Defining extensions on primitive types: handling dates 305

Member extension functions: internal DSL for SQL 306

Anko: creating Android UIs dynamically 309

11.5 Summary 310

Appendix A Building Kotlin projects 313

Appendix B Documenting Kotlin code 317

Appendix C The Kotlin ecosystem 320

Index 323

From the B&N Reads Blog

Customer Reviews