Kotlin is a statically typed language that compiles to JVM bytecode and JavaScript. While Java 8 introduced improvements like lambdas and default methods, Kotlin is still considered more elegant and concise due to features like null safety, ranges, pattern matching, data classes, extension functions, and type-safe builders. Kotlin also supports operator overloading and collection APIs that are covariant by default.
JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović
1. Is there Kotlin after Java 8
Ivan Turčinović & Igor Buzatović
2. What is Kotlin?General
2
Statically typed, JVM-targeted language
Design principles: Industrial use, tooling, safety
Compiles to JVM byte code and JavaScript
Runtime size 800K
3. Current status, plans, release dateGeneral
No release date, no spec yet
Roadmap
M1 → M2 → … → Beta → Jetbrains project → V1.0
Latest milestone → M7
3
Some frameworks/libraries already appeared
Kara Web Framework – Statically typed HTML/CSS
Spek – Specification/test framework
Kannotator – Infer annotations from bytecode
KotlinPrimavera - Kotlin libraries to Spring projects
4. Java strikes back with Java 8
Advantages over Java 7 are overwhelming
Java 8 introduced
Lambdas
General
4
Lambdas
Null safety (“Optional” class)
Default methods
Streams API
New Date & Time API
5. But even with Java 8 Kotlin still...
Is more elegant, concise and safe
General
5
Has more cool stuff
6. Basic syntax and rulesMore elegant?
Functions - definition in
package or in class
Immutable/mutable
variables
No „new” keyword
Type inference
6
Type inference
No checked exceptions
No primitive types
No static members
7. More elegant? Basic syntax and rules cont’d
7
Primary constructors
No fields, just properties
Bean style classes easy to declare
By default, all classes are final
8. More elegant?
Null safety
• Null reference – Billion dollar mistake
• Kotlin is designed in a way that aims to
eliminate NPE from our code
8
9. GenericsMore elegant?
Not co-variant by default, reason?
Wildcards, use-site variance
Co-variance price, producers
Contra-variance, consumers
Bloch: "PECS: Producer extend,
Consumer super“
Java
9
Declaration-site variance
out / in generics modifiers
Supports use-site variance also
No wildcard syntax, out / in used
Kotlin
Java survey: 39% of wildcards usage can be replaced by declaration-site variance
10. Default arguments, named argumentsMore cool stuff
Default argument values can
be defined
Arguments with default
values are optional
No more need for function
overloading (almost)
10
overloading (almost)
Kotlin classes can have only
one constructor
Arguments can be called by
name
When passing arguments by
name ordering doesn’t
matter
11. RangesMore cool stuff
Simpler “is it in range” check
Can be used for any type
that implements
Comparable
“both ends“ included
“..” operator is translated to
“rangeTo” function
11
“rangeTo” function
“rangeTo” function is
implemented as extension
function on Comparable
Numerical ranges can be
iterated over
In both directions and in
arbitrary steps
12. Pattern matchingMore cool stuff
Java’s instaceof not very practical
No !instanceof
Meet Kotlin’s "is“
12
"is" negation "!is“
Automatic type cast when “is”
evaluates true inside if / when
blocks
13. Data classesMore cool stuff
Classes very easy to declare
Data annotation changes
13
Output
javacro.User@41a7d9e7
1101519335
1926426205
false
Data annotation changes
behavior of hashCode,
equals, toString functions
Default implementation uses
all properties
Off course we can override
14. Data classesMore cool stuff
Classes very easy to declare
Data annotation changes
14
Output
User(firstName=John,lastName=Doe)
71819599
71819599
true
Data annotation changes
behavior of hashCode,
equals, toString functions
Default implementation uses
all properties
Off course we can override
15. Operator overloadingMore cool stuff
Operators are translated to
corresponding functions
Operator set is fixed
Operator overloading is achieved by
overriding corresponding functions of
specific class
15
specific class
By adding extension functions we can
enable certain operator for classes that
don’t support it originally
On our own classes we can enable
certain operator by implementing
corresponding functions
16. Extension functionsMore cool stuff
What to do if we want to add features
to existing classes
Java: extending or utility classes
Kotlin: add functions to existing
classes without changing their code
Example: implement BigDecimal +
16
Example: implement BigDecimal +
operator
How does it work “behind the scene”?
17. Extension functionsMore cool stuff
What to do if we want to add features
to existing classes
Java: extending or utility classes
Kotlin: add functions to existing
classes without changing their code
Example: implement BigDecimal +
17
Example: implement BigDecimal +
operator
How does it work “behind the scene”?
18. Kotlin collectionsMore cool stuff
18
Read-only traits on top, co-variant
Extended by mutable traits
Implemented by JDK classes (compile time modifications)
Read-only traits do not guarantee immutability
Top level package functions for instantiating collections:
Read-only: setOf, listOf, mapOf
Mutable: hashSetOff, arrayListOf, hashMapOf
19. Type-safe Groovy-style buildersMore cool stuff
19
For generating tree-like structures (UI layouts, 3D scene graphs)
Tree-like syntax – better insight in what is being generated
Functions create, initialize and return builder specific objects
Better then xml: no scheme required, type safe, interoperable with other code
20. Type-safe Groovy-style buildersMore cool stuff
Objects initialized using
function literal argument
Extension function literal
allows referencing to object
enclosing element
20
enclosing element
Kara web framework using it
for HTML layouts, CSS
Android apps, XML layouts