21
Is there Kotlin after Java 8 Ivan Turčinović & Igor Buzatović

JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović

Embed Size (px)

DESCRIPTION

Kotlin is new JVM language still cooking in Jetbrains kitchen. It looks awesome compared to Java 7, but now, with Java 8 in the field, the question arises: “Does Kotlin still have enough advantages over Java to attract Java developers?” Similar questions could be asked for other JVM languages like Scala, Ceylon, Clojure. We tried to compare new features of Java 8 with corresponding features of Kotlin and see what Kotlin offers beyond features existing in latest Java release.

Citation preview

Page 1: JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović

Is there Kotlin after Java 8

Ivan Turčinović & Igor Buzatović

Page 2: JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović

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

Page 3: JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović

Current status, plans, release date General

� 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

Page 4: JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović

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

Page 5: JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović

But even with Java 8 Kotlin still...

� Is more elegant, concise and safe

General

5

�Has more cool stuff

Page 6: JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović

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

Page 7: JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović

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

Page 8: JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović

More elegant? Null safety

• Null reference – Billion dollar mistake

• Kotlin is designed in a way that aims to eliminate NPE from our code

8

Page 9: JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović

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

Page 10: JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović

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

Page 11: JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović

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

Page 12: JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović

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

Page 13: JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović

Data classesMore cool stuff

� Classes very easy to declare

� Data annotation changes

13

Output

javacro.User@41a7d9e711015193351926426205false

� Data annotation changes behavior of hashCode, equals, toString functions

� Default implementation uses all properties

� Off course we can override

Page 14: JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović

Data classesMore cool stuff

� Classes very easy to declare

� Data annotation changes

14

Output

User(firstName=John,lastName=Doe)7181959971819599true

� Data annotation changes behavior of hashCode, equals, toString functions

� Default implementation uses all properties

� Off course we can override

Page 15: JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović

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 enablecertain operator by implementingcorresponding functions

Page 16: JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović

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 existingclasses without changing their code

� Example: implement BigDecimal +

16

� Example: implement BigDecimal + operator

� How does it work “behind the scene”?

Page 17: JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović

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 existingclasses without changing their code

� Example: implement BigDecimal +

17

� Example: implement BigDecimal + operator

� How does it work “behind the scene”?

Page 18: JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović

Kotlin collections More 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

Page 19: JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović

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

Page 20: JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović

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

Page 21: JavaCro'14 - Is there Kotlin after Java 8 – Ivan Turčinović and Igor Buzatović

Thank You!

21

Thank You!