Upload
hujak-hrvatska-udruga-java-korisnika-croatian-java-user-association
View
1.379
Download
0
Tags:
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
Is there Kotlin after Java 8
Ivan Turčinović & 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
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
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
But even with Java 8 Kotlin still...
� Is more elegant, concise and safe
General
5
�Has more cool stuff
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
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
More elegant? Null safety
• Null reference – Billion dollar mistake
• Kotlin is designed in a way that aims to eliminate NPE from our code
8
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
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
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
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
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
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
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
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”?
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”?
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
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
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
Thank You!
21
Thank You!