SlideShare a Scribd company logo
3  kotlin vs. java- what kotlin has that java does not
3  kotlin vs. java- what kotlin has that java does not
3  kotlin vs. java- what kotlin has that java does not
A function that is not declared, but passed immediately as an expression:
max(strings, { a, b -> a.length < b.length })
As a function, it is equivalent to:
fun compare(a: String, b: String): Boolean = a.length < b.length
fun <T> max(collection: Collection<T>, less: (T, T) -> Boolean): T? {
var max: T? = null
for (it in collection)
if (max == null || less(max, it))
max = it
return max
}
Lambda Expression Syntax:
val sum = { x: Int, y: Int -> x + y }
Using higher-order functions imposes certain runtime penalties:
● each function is an object
● it captures a closure
Memory allocations (both for function objects and classes) and virtual calls
introduce runtime overhead
This kind of overhead can be eliminated by inlining the lambda expressions
lock(l) { foo() }
l.lock()
try {
foo()
}
finally {
l.unlock()
}
inline fun <T> lock(lock: Lock, body: () -> T): T {
// ...
}
inline fun foo(inlined: () -> Unit, noinline notInlined: () -> Unit) {
// ...
}
3  kotlin vs. java- what kotlin has that java does not
Kotlin, similar to C#, provides the ability to extend a class with new functionality
without having to inherit from the class or use any type of design pattern such as
Decorator
This is done via special declarations called extensions
Kotlin supports extension functions and extension properties
fun MutableList<Int>.swap(index1: Int, index2: Int) {
val tmp = this[index1] // 'this' corresponds to the list
this[index1] = this[index2]
this[index2] = tmp
}
val l = mutableListOf(1, 2, 3)
l.swap(0, 2) // 'this' inside 'swap()' will hold the value of 'l'
open class C
class D: C()
fun C.foo() = "c"
fun D.foo() = "d"
fun printFoo(c: C) {
println(c.foo())
}
printFoo(D())
class C {
fun foo() { println("member") }
}
fun C.foo() { println("extension") }
class C {
fun foo() { println("member") }
}
fun C.foo(i: Int) { println("extension") }
3  kotlin vs. java- what kotlin has that java does not
if (obj is String) {
print(obj.length)
}
if (obj !is String) { // same as !(obj is String)
print("Not a String")
}
else {
print(obj.length)
}
"Unsafe"
val x: String = y as String
val x: String? = y as String?
"Safe" (nullable)
val x: String? = y as? String
3  kotlin vs. java- what kotlin has that java does not
The primary constructor is part of the class header: it goes after the class name
(and optional type parameters)
class Person constructor(firstName: String) {
}
The primary constructor cannot contain any code
Initialization code can be placed in initializer blocks, which are prefixed with the
init keyword
class Constructors {
init {
println("Init block")
}
constructor(i: Int) {
println("Constructor")
}
}
3  kotlin vs. java- what kotlin has that java does not
class A {
fun foo() { }
}
class B {
fun call() { }
}
class C {
var b = B()
fun call() {
return b.call()
}
}
interface Base {
fun print()
}
class BaseImpl(val x: Int) : Base {
override fun print() { print(x) }
}
class Derived(b: Base) : Base by b
fun main(args: Array<String>) {
val b = BaseImpl(10)
Derived(b).print() // prints 10
}
3  kotlin vs. java- what kotlin has that java does not
if (i in 1..10) { // equivalent of 1 <= i && i <= 10
println(i)
}
for (i in 1..4) print(i) // prints "1234"
for (i in 4..1) print(i) // prints nothing
for (i in 4 downTo 1) print(i) // prints "4321"
for (i in 1..4 step 2) print(i) // prints "13"
for (i in 4 downTo 1 step 2) print(i) // prints "42"
for (i in 1 until 10) { // i in [1, 10), 10 is excluded
println(i)
}
3  kotlin vs. java- what kotlin has that java does not
Functions that overload operators need to be marked with the operator modifier
Unary prefix operators
Expression Translated to
+a a.unaryPlus()
-a a.unaryMinus()
!a a.not()
data class Point(val x: Int, val y: Int)
operator fun Point.unaryMinus() = Point(-x, -y)
val point = Point(10, 20)
println(-point) // prints "(-10, -20)"
data class Counter(val dayIndex: Int) {
operator fun plus(increment: Int): Counter {
return Counter(dayIndex + increment)
}
}
3  kotlin vs. java- what kotlin has that java does not
data class User(val name: String, val age: Int)
The compiler automatically derives the following members from all properties
declared in the primary constructor:
equals()/hashCode() pair
toString() of the form "User(name=John, age=42)"
componentN() functions corresponding to the properties in their order of
declaration
copy() function

More Related Content

PDF
2 kotlin vs. java: what java has that kotlin does not
PDF
1 kotlin vs. java: some java issues addressed in kotlin
PPTX
Java vs kotlin
PDF
Scala Paradigms
PDF
Demystifying functional programming with Scala
PDF
Let's make a contract: the art of designing a Java API
ODP
Functional Programming With Scala
PDF
Python for data science by www.dmdiploma.com
2 kotlin vs. java: what java has that kotlin does not
1 kotlin vs. java: some java issues addressed in kotlin
Java vs kotlin
Scala Paradigms
Demystifying functional programming with Scala
Let's make a contract: the art of designing a Java API
Functional Programming With Scala
Python for data science by www.dmdiploma.com

What's hot (20)

PPTX
Introduction to kotlin
PDF
Pragmatic Real-World Scala (short version)
PPT
C# programming
PDF
Learning Functional Programming Without Growing a Neckbeard
PDF
Blazing Fast, Pure Effects without Monads — LambdaConf 2018
PDF
Operator Overloading In Scala
PDF
Humble introduction to category theory in haskell
PDF
Introduction to functional programming using Ocaml
PDF
Sneaking inside Kotlin features
PDF
Orthogonal Functional Architecture
PPTX
Scala - where objects and functions meet
PDF
O caml2014 leroy-slides
PPTX
Introduction to kotlin + spring boot demo
PDF
Hey! There's OCaml in my Rust!
PDF
Idiomatic Kotlin
PDF
The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...
PDF
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
PDF
All Aboard The Scala-to-PureScript Express!
PPTX
Java 7, 8 & 9 - Moving the language forward
ODP
Object Equality in Scala
Introduction to kotlin
Pragmatic Real-World Scala (short version)
C# programming
Learning Functional Programming Without Growing a Neckbeard
Blazing Fast, Pure Effects without Monads — LambdaConf 2018
Operator Overloading In Scala
Humble introduction to category theory in haskell
Introduction to functional programming using Ocaml
Sneaking inside Kotlin features
Orthogonal Functional Architecture
Scala - where objects and functions meet
O caml2014 leroy-slides
Introduction to kotlin + spring boot demo
Hey! There's OCaml in my Rust!
Idiomatic Kotlin
The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
All Aboard The Scala-to-PureScript Express!
Java 7, 8 & 9 - Moving the language forward
Object Equality in Scala
Ad

Similar to 3 kotlin vs. java- what kotlin has that java does not (20)

PDF
Bologna Developer Zone - About Kotlin
PDF
Intro to Kotlin
PPTX
KotlinForJavaDevelopers-UJUG.pptx
PPTX
Introduction to Kotlin.pptx
PPTX
01 Introduction to Kotlin - Programming in Kotlin.pptx
PDF
Privet Kotlin (Windy City DevFest)
PDF
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
PDF
Kotlin Developer Starter in Android projects
PDF
Kotlin for Android Developers - 3
PDF
かとうの Kotlin 講座 こってり版
PDF
Kotlin cheat sheet by ekito
PDF
Feel of Kotlin (Berlin JUG 16 Apr 2015)
PDF
Kotlin for Android Developers - 2
PPTX
Introduction to Kotlin
PDF
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
PPT
The Kotlin Programming Language
PDF
Bulletproofing your foot for Kotlin
PPTX
K is for Kotlin
PDF
Kotlin Basics - Apalon Kotlin Sprint Part 2
Bologna Developer Zone - About Kotlin
Intro to Kotlin
KotlinForJavaDevelopers-UJUG.pptx
Introduction to Kotlin.pptx
01 Introduction to Kotlin - Programming in Kotlin.pptx
Privet Kotlin (Windy City DevFest)
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android projects
Kotlin for Android Developers - 3
かとうの Kotlin 講座 こってり版
Kotlin cheat sheet by ekito
Feel of Kotlin (Berlin JUG 16 Apr 2015)
Kotlin for Android Developers - 2
Introduction to Kotlin
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
The Kotlin Programming Language
Bulletproofing your foot for Kotlin
K is for Kotlin
Kotlin Basics - Apalon Kotlin Sprint Part 2
Ad

3 kotlin vs. java- what kotlin has that java does not

  • 4. A function that is not declared, but passed immediately as an expression: max(strings, { a, b -> a.length < b.length }) As a function, it is equivalent to: fun compare(a: String, b: String): Boolean = a.length < b.length
  • 5. fun <T> max(collection: Collection<T>, less: (T, T) -> Boolean): T? { var max: T? = null for (it in collection) if (max == null || less(max, it)) max = it return max } Lambda Expression Syntax: val sum = { x: Int, y: Int -> x + y }
  • 6. Using higher-order functions imposes certain runtime penalties: ● each function is an object ● it captures a closure Memory allocations (both for function objects and classes) and virtual calls introduce runtime overhead
  • 7. This kind of overhead can be eliminated by inlining the lambda expressions lock(l) { foo() } l.lock() try { foo() } finally { l.unlock() } inline fun <T> lock(lock: Lock, body: () -> T): T { // ... }
  • 8. inline fun foo(inlined: () -> Unit, noinline notInlined: () -> Unit) { // ... }
  • 10. Kotlin, similar to C#, provides the ability to extend a class with new functionality without having to inherit from the class or use any type of design pattern such as Decorator This is done via special declarations called extensions Kotlin supports extension functions and extension properties
  • 11. fun MutableList<Int>.swap(index1: Int, index2: Int) { val tmp = this[index1] // 'this' corresponds to the list this[index1] = this[index2] this[index2] = tmp } val l = mutableListOf(1, 2, 3) l.swap(0, 2) // 'this' inside 'swap()' will hold the value of 'l'
  • 12. open class C class D: C() fun C.foo() = "c" fun D.foo() = "d" fun printFoo(c: C) { println(c.foo()) } printFoo(D()) class C { fun foo() { println("member") } } fun C.foo() { println("extension") } class C { fun foo() { println("member") } } fun C.foo(i: Int) { println("extension") }
  • 14. if (obj is String) { print(obj.length) } if (obj !is String) { // same as !(obj is String) print("Not a String") } else { print(obj.length) }
  • 15. "Unsafe" val x: String = y as String val x: String? = y as String? "Safe" (nullable) val x: String? = y as? String
  • 17. The primary constructor is part of the class header: it goes after the class name (and optional type parameters) class Person constructor(firstName: String) { }
  • 18. The primary constructor cannot contain any code Initialization code can be placed in initializer blocks, which are prefixed with the init keyword class Constructors { init { println("Init block") } constructor(i: Int) { println("Constructor") } }
  • 20. class A { fun foo() { } } class B { fun call() { } } class C { var b = B() fun call() { return b.call() } }
  • 21. interface Base { fun print() } class BaseImpl(val x: Int) : Base { override fun print() { print(x) } } class Derived(b: Base) : Base by b fun main(args: Array<String>) { val b = BaseImpl(10) Derived(b).print() // prints 10 }
  • 23. if (i in 1..10) { // equivalent of 1 <= i && i <= 10 println(i) } for (i in 1..4) print(i) // prints "1234" for (i in 4..1) print(i) // prints nothing for (i in 4 downTo 1) print(i) // prints "4321" for (i in 1..4 step 2) print(i) // prints "13" for (i in 4 downTo 1 step 2) print(i) // prints "42" for (i in 1 until 10) { // i in [1, 10), 10 is excluded println(i) }
  • 25. Functions that overload operators need to be marked with the operator modifier Unary prefix operators Expression Translated to +a a.unaryPlus() -a a.unaryMinus() !a a.not() data class Point(val x: Int, val y: Int) operator fun Point.unaryMinus() = Point(-x, -y) val point = Point(10, 20) println(-point) // prints "(-10, -20)" data class Counter(val dayIndex: Int) { operator fun plus(increment: Int): Counter { return Counter(dayIndex + increment) } }
  • 27. data class User(val name: String, val age: Int) The compiler automatically derives the following members from all properties declared in the primary constructor: equals()/hashCode() pair toString() of the form "User(name=John, age=42)" componentN() functions corresponding to the properties in their order of declaration copy() function