Welcome to the Klassbook!
283 Kotlin Programming Lessons, In Your Browser
CommonsWare's Klassbook is a place for you to learn Kotlin by running and tinkering with Kotlin code directly in your Web browser.
The Klassbook exists to support CommonsWare's books:
Start at the BeginningThe Lessons
- Hello World
- Basic Types
- Variables
- Functions
- Collections
- Arrays
- Typed Arrays
- Lists
- Sets
- Maps
- Combining Collections
- Accessing Elements via []
- Array Modification
- Mutable Lists
- Lambda Expressions
- Lambda Expressions and Parameters
- forEach()
- forEach() is a Function
- for Loop
- filter()
- map()
- map() Type Conversions
- Varargs
- Values for Varargs
- Spread Operator
- Spread Operator with Primitive Types
- Branches and Loops
- if
- if Without else
- Single-Line if
- else if
- when
- when Works with Any Type
- Capturing Values in when
- when Without an Expression
- when with Commas
- when with Ranges
- when with Expressions for Branches
- while
- if as an Expression
- if as an Expression, Using Blocks
- when as an Expression
- break Statements
- continue Statements
- Classes
- Classes and Instances
- Adding Functions to Classes
- Adding Properties to Classes
- The this Pseudo-Property
- Passing this as a Parameter
- Constructors
- Formal Constructor Declaration
- Init Block
- Constructor Init-Only Parameters
- Secondary Constructors
- Inheritance
- Inheritance and Constructors
- Inheritance and Functions
- Inheritance and Overriding Functions
- override is Transitive
- Overriding Properties
- Chaining to Superclass Functions
- Checking Inheritance Via is
- is and when
- Smart Casts
- Manual Casts
- Data Class
- Properties
- Visibility
- Scope
- Abstract Classes and Interfaces
- Object
- Nested Objects and Classes
- Enums and Sealed Classes
- Generics
- Exceptions
- Nullability
- Scope Functions
- Functional Programming
- first()
- first() Sans Lambda
- firstOrNull()
- firstOrNull() and the Elvis Operator
- Chaining Higher-Order Functions
- any()
- any() and Short Circuits
- fold()
- joinToString()
- Function Types
- Function Reference
- Function Reference for Member Functions
- Function References and Receivers
- Function Types and Direct Invocation
- Function Types and Parameters
- Type Aliases
- Functional Interfaces
- Sequence
- Extension Functions
- Delegates
- Coroutine Basics
- A Simple Coroutine Sample
- Suspending main()
- A Sequential Coroutine Sample
- A Parallel Coroutine Sample
- Massively Parallel Coroutines
- async()
- runBlocking()
- promise()
- Contexts and Jobs
- Jobs and Parents
- Lazy Coroutines
- Starting Lazy Coroutines
- join()
- Canceling a Job
- cancelAndJoin()
- Canceling a Parent Cancels Its Children
- Jobs Cancel When They Crash
- Parent Jobs Cancel When a Child Crashes
- Timeouts
- TimeoutCancellationException
- withTimeoutOrNull()
- Canceling and Cooperation
- Cooperation by Yielding
- Cooperation by Checking isActive
- Non-Cancellable Coroutines
- invokeOnCompletion()
- Flows and Channels
- A Simple Flow
- flowOf()
- single() and singleOrNull()
- first() and Flow
- toList() and toSet()
- suspend Functions and Flows
- flowOn()
- Flow Actions
- Flow onStart()
- Flows and Exceptions
- retry()
- retryWhen()
- catch()
- A Simple Channel
- Hot Channels Via offer()
- The Channel() Function
- toList() for Channel
- Channel and receive()
- A Zero-Buffer Channel
- A Fixed-Buffer Channel
- A Conflated Channel
- Channels and Fan-out
- BroadcastChannel
- A Simple SharedFlow
- MutableSharedFlow Replay Option
- A Simple StateFlow
- StateFlow and Content Equality
- combine() Operator
- distinctUntilChanged() Operator
- distinctUntilChangedBy() Operator
- debounce() Operator
- drop() Operator
- dropWhile() Operator
- filter() Operator
- filterIsInstance() Operator
- filterNot() Operator
- filterNotNull() Operator
- flattenConcat() Operator
- flattenMerge() Operator
- fold() Operator
- map() Operator
- mapLatest() Operator
- mapNotNull() Operator
- sample() Operator
- scan() Operator
- take() Operator
- takeWhile() Operator
- withIndex() Operator
- zip() Operator
- Advanced Syntax
- Custom Getter Functions
- Custom Setter Functions
- Custom Accessors and the Field
- Inline Custom Getter Functions
- Custom Accessor Parallelism
- Extension val Property
- Extension var Property
- Destructuring Declarations
- Destructuring Declarations of Maps
- Destructuring Declarations and Lambdas
- Operator Overloading
- invoke() Operator
- Infix Functions
- Anonymous Functions
- Anonymous Functions as Parameters
- Inline Functions
- Inline Properties
- Abstract val Properties
- Abstract val Custom Getter
- Abstract var
- Default return Behavior
- return and Anonymous Functions
- Labeled Returns
- Custom Labels
- Nothing As a Return Type
- Reified Types
- Class Delegates