Is there Kotlin after Java 8 Ivan Turčinović & Igor Buzatović What is Kotlin? Statically typed, JVM-targeted language Design principles: Industrial use, tooling, safety Compiles to JVM byte code and JavaScript Runtime size 800K 2 General General No release date, no spec yet Roadmap M1 → M2 → … → Beta → Jetbrains project → V1.0 Latest milestone → M7 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 3 Current status, plans, release date General Advantages over Java 7 are overwhelming Java 8 introduced Lambdas Null safety (“Optional” class) Default methods Streams API New Date & Time API 4 Java strikes back with Java 8 But even with Java 8 Kotlin still... Is more elegant, concise and safe Has more cool stuff 5 General Basic syntax and rules Functions - definition in package or in class Immutable/mutable variables No „new” keyword Type inference No checked exceptions No primitive types No static members 6 More elegant? Basic syntax and rules cont’d Primary constructors No fields, just properties Bean style classes easy to declare By default, all classes are final 7 More elegant? More elegant? Null safety • Null reference – Billion dollar mistake 8 • Kotlin is designed in a way that aims to eliminate NPE from our code More elegant? Java Generics Not co-variant by default, reason? Wildcards, use-site variance Co-variance price, producers Contra-variance, consumers Bloch: "PECS: Producer extend, Consumer super“ Kotlin Declaration-site variance out / in generics modifiers Supports use-site variance also 9 No wildcard syntax, out / in used Java survey: 39% of wildcards usage can be replaced by declaration-site variance Default arguments, named arguments Default argument values can be defined Arguments with default values are optional No more need for function overloading (almost) Kotlin classes can have only one constructor Arguments can be called by name When passing arguments by name ordering doesn’t matter 10 More cool stuff Ranges Simpler “is it in range” check Can be used for any type that implements Comparable “both ends“ included “..” operator is translated to “rangeTo” function “rangeTo” function is implemented as extension function on Comparable Numerical ranges can be iterated over In both directions and in arbitrary steps 11 More cool stuff Pattern matching Java’s instaceof not very practical No !instanceof Meet Kotlin’s "is“ "is" negation "!is“ Automatic type cast when “is” evaluates true inside if / when blocks 12 More cool stuff More cool stuff Data classes Classes very easy to declare Data annotation changes behavior of hashCode, equals, toString functions Default implementation uses all properties javacro.User@41a7d9e7 1101519335 1926426205 false Off course we can override 13 Output More cool stuff Data classes Classes very easy to declare Data annotation changes behavior of hashCode, equals, toString functions Default implementation uses all properties User(firstName=John,lastName=Doe) 71819599 71819599 true Off course we can override 14 Output Operator overloading Operators are translated to corresponding functions Operator set is fixed Operator overloading is achieved by overriding corresponding functions of 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 15 More cool stuff Extension functions 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 + operator How does it work “behind the scene”? 16 More cool stuff More cool stuff Kotlin collections 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 17 More cool stuff Type-safe Groovy-style builders 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 18 For generating tree-like structures (UI layouts, 3D scene graphs) Type-safe Groovy-style builders Objects initialized using function literal argument Extension function literal allows referencing to object enclosing element Kara web framework using it for HTML layouts, CSS Android apps, XML layouts 19 More cool stuff 20 Thank You!