Kotlin is the new kid on the block. We compare its language features to other popular languages such as Java and Python. We focus on how Kotlin constructs strike a balance between brevity, clarity, and type-safety.
2. Biography (boooooring!)
- VP of Engineering at Webalo
- Los Angeles based
- UX4IIoT
- 20 years of software development
- PhD. in software engineering
3. Programming Languages
Pascal C C++
Java
Python C# Objective-C ActionScript
Scala Ruby Octave
JavaScript
Go TypeScript Kotlin
BASIC
4. Kotlin Arrives!
- Paid little attention until Google I/O announcement.
- Android Studio Canary 3.
- Immediately fell in love
- Strongly-typed
- Brief
- Expressive
- Interoperable with Java
- Growing support
- Starts from where others ended (Java, Python, Go, Swift)
6. Brevity
In Java, one would normally write this:
With my background, I translated this into Kotlin verbatim:
Not idiomatic but still
shorter! Can you spot
the omissions?
● No ;
● No “new”
● “override” is a modifier
● “:” not “implements”
● “public” is default
● Return type
In Java one would typically write code like this:
8. Brevity
Omit parameter type
Omit parameter name as well
Eliminate left hand side completely
Lambda expression only argument so we can eliminate the parenthesis
Omit parameter type
12. Null Safety
Handle via Java 8 Optionals - very verbose!
Handle by convention:
- Special member naming (IfAny)
- Null checks in constructor
- Hand-coded
- AspectsOr let it throw! (pun intended)
16. Conditionals
In Kotlin, if statement is an expression
Combined with single expression function shorthand
So you can write a function like this
17. Conditionals
I hate “Switch” statements!
But I love “when” expressions!
1. No annoying “break” or even “case”
2. Match against any type
3. Switch variable can be of any type
18. We will go through:
Brevity
Null Safety
Conditionals
Smart Cast
Lambdas
Collections
19. Smart Cast
In Java explicit cast is needed
In Kotlin Smart Cast takes place automatically
30. Collections - Immutability
In Java both mutable and immutable collections implement the same interface - Runtime error
Kotlin uses different interfaces - Compile error