SlideShare a Scribd company logo
A Brief Language Overview
Ed Austin
ed@ryer.ru
Hello! Привет!
Developer
ed@ryer.ru
+44 7726 05 0000
+7 925 871 9411
I am Ed Austin Я Эд Остин
Презентация будет на английском
Disclaimer!
Liable to have mistakes!
Don’t hesitate to point them out!
Part 1 Some Background
What is Kotlin and how does it differ to Java?
1
What is in the Name?
Named after an Island off
the coast of St.
Petersburg*
*The second largest city in the Russian Federation and the home of JetBrains
© Ed Austin 2017
History of Kotlin?
2010: JetBrains started the Kotlin Project
2011: Public Announcement
2012: Open Sourced under Apache 2
2016: Release 1.0
2017: Release 1.1 Google announced first class language support
2018: Release 1.2
Release 2.0?? No indication but probably 2019+
._X = bug fixes e.g. 1.02
_.X = incremental, no major changes e.g. 1.1, 1.2
X.0 = major (may introduce major features, deprecate others) e.g. 2.0
What exactly is Kotlin?
Java-like with Clean abstractions,
concise syntax, and type safety.
Full Java interoperability,
significant tool chain from the start.
Fully open-source (Apache 2).
Sugared Java
Java++
Java on Steroids
Scala for idiots
The "PL/1" of our times
💣 Depending on your point of view the Language can be described in various ways!
Negative View Positive View
© Ed Austin 2017
What/Who influenced Kotlin?
😉 Disclaimer: I did ask Andrey Breslav (Language Creator at JB)
Josh Bloch
Grady Booch
Kent Beck
Erich Gamma
Martin Fowler
Groovy, Java, Scala…
Effective Java
OOD
TDD + Smalltalk stuff
GoF
OOD/UML
Smalltalk, Algol 60, Simula 67
Language Viewpoint
Imperative?
Functional?
Object Oriented?
From a purists point of view no, but
then again neither are Java or C++
So from a language classification viewpoint it can be viewed in much the same way as Java.
© Ed Austin 2017
Supported Architectures
Kompiler + Tool support for:
Android (ART) – Google Official Support
Likely to become the dominant mobile applications language for AOSP within a few years
JVM 1.6+ (later JVM may support better optimizations, not yet Valhalla EAB)
Native (x86/ARM/Others)
Transpiles to JS
Basically in the long-term to be running on as many platforms as possible
© Ed Austin 2017
What differs from Java
Non-nullable Type System
All types are non-nullable by
default (but can and may have to
be nullable in some
circumstances)
No Covariant Arrays
Covariant arrays are gone, now
invariant.
No Primitives
Everything in Kotlin is an
Object.
No Checked Exceptions
Don’t exist in the class
hierarchy of Errors.
No Statics
No statics exist in the language,
at least conceptually, but likely
to change
No Raw types
No need for backward JVM
compatibility, we also have
reification!
AND Top level properties and funs, Data Classes, Co-routines, Smartcasting, Operator Overloading,
Properties, Extension Functions, Companion Objects, Mutable/Immutable collections, Singletons, String
templates, Range expressions, Delegation, Generics … NO SEMICOLONS!!!
Some of what it doesn’t have…
Multi-Catch
For Exception handling
True immutability
Same implementation as Java
reference types
Value Types
Beyond simple JVM types
JVM limitation?
PP Visibility/Namespaces
For use in Java interoperability.
Array Slices
data[a:b], data[:b] etc.
SAM Conversion
Only for Java not for Kotlin
interfaces
No Boolean Op Overloading
All other operators are however
overloadable
Classes, Objects & Inheritance
Basics
2
Class Hierarchy - Any and Nothing
Any
Equivalent to java.lang.Object*
Nothing
Is not Void! (Unit type is Void)
Note:Technically Object is actually
a subtype of Any (TBD)
Diagram courtesy of JetBrains
Interface and Implementation Inheritance
private interface IfMyInterface : IfSomeOtherInterface{
val x get() = 100
fun someMethoda(x: Any?) : Unit /* contract unfulfilled */
fun someMethodb(x: Int) : Int = 123 /* Java 8’ish */
}
open class SomeClass /* open for extension – final by default! */
abstract class SomeAbstractClass : Any()
final class MyClass : SomeClass(), IfMyInterface {
override fun someMethoda(x: Any?) {
/* body */
}
}
class SubClass : SomeAbstractClass() {
/* implement the abstract class method bodies */
}
Note:
Syntax is very
close to Java and
readable by any
Java developer.
Class Basics
//constructor keyword optional
//default parameters
public class TopLevelClass private constructor(val name:String="unknown", val age:Int =-1) {
var t = 10
//secondary C
constructor(name:String, age:Int, gender:String ="unknown") : this(name, age) {}
//tripe, just to show local instantiation
inner class InnerClass(){
val innert = t
val tlc = TopLevelClass("jane")
}
class AClass{
val nestedt = t
fun showme() = 123
}
}
/* … */
/* call with a named parameter, and using default parameters */
val tlc = TopLevelClass("john", 22, gender="male") //call 3-arg C
Sealed Class
abstract sealed class Sealy { /* default private C */
class Red : Sealy()
class Green : Sealy()
}
/* … */
val sealy : Sealy = Sealy.Red()
val output = when (sealy) {
is Sealy.Red -> "red"
is Sealy.Green -> "green"
/* no else branch! */
}
println("result is ${output}") //shortened println syntax and String templates
Primary benefit: Exhaustive case when expression. Specified set of specific values (such as a state
machine). Similar to an enum class, effectively abstract so not instantiable.
Data Class
data class Student(val name: String, var age: Int)
This is the entire class (no body)!
getters and if applicable setters autogenerated
hashcode/equals pair for the object (structural equivalence)
Custom toString (in the form of (name=John, age=30) )
Restriction: Can’t be opened for extension, abstract, sealed or inner.
Copy extension function (with optional modification)
val stu = Student("ed", 42)
val stu_amended_copy = stu.copy(age=43) //not referential!
Kotlin Singleton via object
© Ed Austin 2017
// Java
public class SingletonDcl {
private static SingletonDcl instance = null;
private SingletonDcl(){
}
private synchronized static void createInstance() {
if (instance == null) {
instance = new SingletonDcl();
}
}
public static SingletonDcl getInstance() {
if (instance == null) createInstance();
return instance;
}
public void foo() {}
}
SingletonDcl.getInstance().foo();
//Kotlin Thread-safe version
public object Singleton {
public fun foo() {}
}
Singleton.foo()
Anonymous Class
var clickCount = 0
window.addMouseListener(object : MouseAdapter() {
override fun mouseClicked(e: MouseEvent) {
// ...
clickCount++ //true closure!
}
override fun mouseEntered(e: MouseEvent) {
// ...
}
})
Unlike older versions of Java…
Companion Objects
Quite possible at 2.0 will be deprecated in favour of static objects
Possibly better to write top level functions and variables before the class rather than use a
Companion Object- elizarov JetBrains Team Aug ’17 (most of the time.. No @JvmStatic)
public class Penka {
val id: Int? = null
init {
id = nextId?.inc()
}
private companion object : SomeClass
var nextId : Int? = 1
fun masha() {}
}
}
fun main(args: Array<String>) {
Penka()
println(Penka().id!!)
}
//can inherit!
public class Lenka {
Integer id;
{
id = nextId++;
}
private static Integer nextId = 1;
static void masha{}
public static void main(String[] args) {
new Lenka();
System.out.println(new Lenka().id);
}
}
var nextId : Int? = 1
fun masha() {}
//above package namespace
public class Penka {
val id: Int? = null
init {
id = nextId?.inc()
}
private companion object {
var nextId : Int? = 1
fun masha() {}
}
}
fun main(args: Array<String>) {
Penka()
println(Penka().id!!)
}
Companion Objects
Simply put a companion object is simply a final static method!
File: MyTest.kt
package foo
class Penka {
private companion object {
fun masha() {
/* some code */
}
}
}
public final class MyTestKt {
public static final void masha() {
/* our code */
}
}
Top Level Properties and Functions
We can have properties and methods at Top level
These are compiled down to JVM static methods within YourPackageName.class (delegates implementations to the so-named package-
parts)
Diagram courtesy of JetBrains
Class Delegation
Smart Casts
fun demo(x: Any) {
if (x is String) {
print(x.length) // x is automatically cast to String
}
}
if (x !is String) return
print(x.length) // x is automatically cast to String
Safe “as?” /Unsafe “as” Cast
val x: String? = y as? String //safe cast.. return null if cast not possible
val x: String = y as String //unsafe cast, may throw CCE!
Extension Functions (add a method to any class)
Extension fun:
fun Int.appendString(s:String) = this.toString() + s 123.appendString("abc") 123abc
infix fun Int.appendString(s:String) = this.toString() + s 123 appendString "abc" 123abc
EBNF: [Infix] fun MyClassTobeExtended.myNoneOverridenMethod([arg]) [: returnType ] = expression | [{ /* block */ }]
In the result refer to the first argument as the receiver with this, and the arg as the parameter sent with the message
Likely a better way to do this by overloading :
operator fun Int.plus(s:String) = this.toString() + s 123 + "abc" 123abc
Java.lang.Integer
For obvious reasons doesn’t have an appendString method*
*I stand slightly corrected, "+“ is overloaded as to: String s = 123 + "abc“ which makes this example pretty useless
Note: We can’t override existing methods
Standard Functions on Any (let, apply, run)
public inline fun <T, R> T.run(block: T.() -> R): R = block()
myClassInstance.run { //we could also use a property and apply a method such as length
this.myVar = 123
this.funkyFunc(123)
} //returns the result of the block (last arg) and mutates the receiver
public inline fun <T> T.apply(block: T.() -> Unit): T { block(); return this }
val person = myPersonInstance.apply {
this.name = "lenka penka"
this.age = 33
} //Calls the specified function [block] with "this" value as its receiver and returns "this" value.
//mutates receiver
public inline fun <T, R> T.let(block: (T) -> R): R = block(this)
"something here".let {println("The length of this String is ${it.length}")
} //Calls the specified function [block] with "this" value as its argument and returns its result.
Standard Functions on Any (let, apply, run)
if (listz.run {
this.add(11)
this.add(12)
} //mutates receiver and returns the last result (true)
){
listz.apply {
this.remove(1)
} //mutate and return receiver
.let { println(it) } //pass and print the receiver (implicit it)
}
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
val listz = arrayListOf<Int>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Standard Functions on Any (Let, Apply. Run)
Modifiers
Visibility & Access
3
Visibility Modifiers
Classes, objects, interfaces, constructors, functions, properties and their setters can have visibility modifiers
Top Level
Functions, properties and classes, objects and interfaces
public by default
private “file level” visibility
internal same module (i.e. PP)
Class Level
Members declared within a Class
public by default
private for scope inside class (and its members*)
internal same module (i.e. PP)
protected class + subclasses
*Outer cannot see private members of its inner classes,
nor protected, although you can extend an open inner class
within the original class to gain access to the protected
members!
Access Modifiers
Classes are…
final by default
open can be extended
package foo
open class MyClass
class bar : MyClass()
final class MyClass
class foo : MyClass()
By default classes cannot be extended (are final), this includes inner classes
Visibility/Access Modifiers Example
// file name: Example.kt
package foo
/* top level! */
private fun foo() {} // visible inside example.kt
public var bar: Int = 5 // property is visible everywhere
private set // setter is visible only in example.kt
internal val baz = 6 // visible inside foo
open class MyClass(){ //not default
private final inner class Inner(){} //reference to outer class
public final class Nested(){} //no refs to outer class
public final object Objy{} //effectively static/singleton
}
Types
Basic Types
4
Java -> Kotlin Primitive/Wrapped Mapping
Kotlin Properties
Abstraction over fields
Can be seen as a “front end” for any backing field
Allows custom getters/setters for variables
We can use expressions on evaluation
Transparent to developers (get default implementation)
Many developers may never use them (using the hidden default implementation)
Allows Custom access
We can add specific visibility modifiers such as public for get and private for set
© Ed Austin 2017
These are the equivalent of Java fields
Kotlin Properties
[<modifiers>] var|val <propertyName>[: <PropertyType>] [= <property_initializer>]
[<getter>]
[<setter>]
var someProperty: String = "defaultValue“ val x
get() = field get() = z + 100
private set(value) { field = value }
© Ed Austin 2017
We can modify the getter/setter of the property with an expression evaluated on set/get as well as visibility modifiers.
A val doesn’t obviously have a setter as it can’t mutate! Wait a minute…..
val someProperty2: String = "defaultValue"
public get() = field + random.nextInt()
Kotlin Example Java Mapping
class Student(val name: String, uni:String)
fun main(args: Array<String>) {
val a = 123
val b = 444f
val c = "ABC"
val d = ("[A..Z]").toRegex()
val e = 1..100
val f = Student("Masha", “MGU")
val g = mutableListOf<Student>()
val h = hashMapOf<Int,Student>()
val i = arrayOf(a,b,c,d,e,f,g,h)
i.forEach { println(it.javaClass.name) }
}
Types are mostly inferred by the Type system, and need not be specified, most underlying types are
from the JDK, the few from Kotlin are additional types
Managing Null Pointers
Design goal was to eliminate NPE as far as possible
Allegedly NPE was/is a huge issue for Java Developers.
Type system rigorously enforces non-nullability
Heavy static typing makes sure you don’t get into trouble
All types have a nullable alternative
We can specify a null type if we really wish
Nullability was not entirely eliminated
Any interoperability with Java brings with it the nullability issue
© Ed Austin 2017
Nullable Type System
Biggest Kotlin difference: A revised Type System that enforces non-nullability by default
var x = 100f /* non-nullable */
var y : Int? = 100 /* this is nullable */
var g : MutableList<Student>? = mutableListOf<Student>()
We simply add “?” After a type definition to indicate the type is
nullable, we must explicitly state (most) nullable types.
IntelliJ picks up static semantic abuse issues and forces use of various
operators to avoid R/T NPE.
Comparing Types
Referential Equality
=== (negated !==)
Points to same object
i.e. shallow
Value/Structural Equality
== (negated !=)
Points to same data
i.e. deep
a?.equals(b) ?: (b === null)
If a not null call equals(b:Any?) and return result
otherwise if a is null (using elvis) return b===null
(referential) i.e. null == null (true). Or null = not null
(false)
The above are used to provide safety working with nullable types in a type system with mixed typing
No "==" or ". equals()" confusion
Java == Java .equals
Some Type System Operators
Safe Call Operator ?.
Allows a check for
receiver nullability before
allowing access to a
method else return null if
the receiver is null
Elvis Operator ?:
Return a non-null
expression on the left
else return a value on
the right
Double Bang Operator !!
Converts any value to a non-null
type and throws an NPE if null
Example:
var foo : String? = "bar"
/* … */
val len = foo?.length()
Example:
var foo: String? = "bar"
/* … */
val len = (foo?.length) ?: -1
Example
var foo: String? = "bar"
/* … */
val len = foo!!.length
The above are used to provide safety working with nullable types in a type system with mixed typing
Safe Cast as?
Cast returns null and
not CCE if the cast fails
Example:
val aInt: Int? = a as? Int
Nullable Types Woes #1
i++
Java
i++
Kotlin Non-nullable
i?.++ (you think?)
i?.inc() (my second guess)
wtf? No side-effects!
Receiver doesn’t mutate!
Kotlin Nullable
Types Woes #2
Thankyou Ilya Ryzhenkov from JetBrains Kotlin Libraries Team for the overridden operator code.
operator fun Int?.inc() : Int? = this?.inc()
fun test() {
var i: Int? = null
i++
}
i++ (for nullables)  i = if (i == null) null else i.inc()
Nullable
Functions/Control Structures
Idiomatic structures and expression usage
5
Some function Signatures
Java Kotlin
String jrasl() { return “abc” } fun justReturnAStringLiteral : String = “abc”
<T> void f(T a, Object b) fun <T> f(a:T, b:Any?) : Unit
<E> void a(Collection<? extends E> items) fun <E> a(items: Collection<out E>) : Unit
void checkConnection() throws IOException No equivalent
No equivalent fun willNeverReturn(): Nothing {
throw IOException()
}
Imperative Structures
Java Kotlin (can be expression)
for(int i=0;i<=10;i++) for(i in 0..10) //i implicit decl/local scope
while (a>0) {/*..*/} while (a>0) {/*..*/}
do { block } while (condition) do { block } while (condition)
String res; val res = if (x > 10) "foo" else "bar"
if (x>10) res="foo";
else res="bar";
if ((x>0) && (x<100)) if (x in 1..100)
for (MyType s : collection) for (item in collection)
switch - case when {...} / when(..) {...} etc
Communicating with Java
A Tour of Kotlin
6
(Nullable) Platform Types
Java composite types have potential nullable references!
That is all reference types in the language (anything other than the basic value types)
When calling to Java we need to prepare for this!
Because we most often are passing back something that could cause us some issues
These types are called “Platform Types” in Kotlin
They are a special type and can cause us NPE when dereferenced because they are nullable
We can use Java-side annotations to pass information
Such as whether to treat the passed reference as @nullable etc.
Platform Types (from Java to Kotlin)
fun main(args: Array<String>) {
val mjt = MyJavaTest()
//what is the dereference result?
println(mjt.getBaddy())
}
public class MyJavaTest{
private Integer baddy;
/* … */
public Integer getBaddy(){
return baddy;
}
}
What happens if we call the Java getter from Kotlin?
Java Kotlin
© Ed Austin 2017
Platform Types (from Java to Kotlin)
fun main(args: Array<String>) {
val mjt = MyJavaTest()
//what is the dereference result?
println(mjt.getBaddy()?:"invalid state!")
}
public class MyJavaTest{
private Integer baddy;
/* … */
public Integer getBaddy(){
return baddy;
}
}
What happens if we call the Kotlin code from Java?
Java Kotlin
© Ed Austin 2017
From Kotlin to Java
Kotlin
//@JvmOverloads allows overloaded Constructors/funs from Java
class TopLevelClazz @JvmOverloads constructor( val name:String="unknown",
val age:Int =-1,
val gender:String="unknown") {}
Java
TopLevelClazz tlc1 = new TopLevelClazz("ed");
System.out.println(tlc1.getAge()); //auto-generated getter from Kotlin
TopLevelClazz tlc2 = new TopLevelClazz("ed",43);
TopLevelClazz tlc3 = new TopLevelClazz("ed",43,"male");
Types (from Kotlin to Java)
import penka.Lenka;
class Penka {
/* … */
new Lenka().name;
//@JvmStatic //Instance
Lenka.foo(); Lenka.MyExposed.foo();
Lenka.bar(); Lenka.MyExposed.bar();
w/o name: Lenka.Companion. …
package penka
final class Lenka {
@JvmField
private var name = "hello world“
private set
companion object MyExposed {
@JvmStatic fun foo() {}
fun bar() {}
}
}
Java VisibilityKotlin
© Ed Austin 2017
@JvmStatic will create a proxy static class (so seen as a static class from Java) that
calls the Kotlin object singleton instance.
Kotlin Exceptions
//Kotlin side
@Throws(IOException::class)
fun foo() : Nothing {
throw IOException()
}
No mandatory handing in Kotlin but
We can if we wish same as Java with try-
catch
//Java side does the call, becomes checked and
forces handling
try {
MyKotlinTest().foo();
}
catch (IOException e) {
//…//
} finally {
//…//
}
Collection and Generics
PECS vs CIPO!
7
Collection/Generics in Kotlin
Collection has Mutable and immutable interfaces that branches off into concrete
implementations. Map has a separate interface (like JDK MutableMap extending Map)
Generics are split into covariant/invariant/contravariant signatures using the out and in
keywords at declaration-site. No more extends/super spaghetti! (Use where for
multiple constraints).
Uses the JDK concrete implementations under the hood:
@SinceKotlin("1.1")
@kotlin.internal.InlineOnly
public inline fun <T> mutableListOf(): MutableList<T> = ArrayList()
@SinceKotlin("1.1") public typealias ArrayList<E> = java.util.ArrayList<E>
Kotlin Collection Hierarchy
Generics Conversion
Invariant
Kotlin: <T>
Java: <T>
Example: Collection
concrete classes are
invariant
Covariant (Producer)
Kotlin: <out E>
Java: <? Extends E>
Upper bounded Example:
Collection interfaces are
covariant
Contravariant (Consumer)
Kotlin: <in E>
Java: <? Super E>
Lower bounded Example: Comparable
interface is contravariant (on declaration-
site in Kotlin)
Remember PECS = CIPO
Wildcard
<*> <out Any?>
<?> <? extends Object>
Wildcard example: iterate/read
through a list of any type
Note: No wildcards in Kotlin Generics!
Generics Conversion
Java:
//JSDK: public interface Comparable <T>
//Effective Java it. 28 recommends
//<T extends Comparable<? super T>>
//<T extends Comparable<T>>
<T extends Comparable<? super T>> void sortz(List<T> x){
sort(x);
}
Kotlin:
//KSDK: public interface Comparable<in T>
fun <T:Comparable<in T>> sortz(x :List<T>){
sort(x)
}
Remember PECS = CIPO
Kotlin:
fun processListz(l : MutableList<*>) {
l.add(22)
for(item in l) println(item)
}
val l = mutableListOf<Any>(123, "abc")
processListz(l)
Java:
void processListz(List<?> l) {
l.add(22)
for(Object item : l)
System.out.println(item);
}
/* ... */
List<Object> l = new ArrayList<Object>();
l.add(123);
l.add("abc");
processListz(l);
*Warning:(16, 25) Kotlin: Projection is redundant: the corresponding type parameter of Comparable has the same variance
Collection Generics at Declaration-site
JAVA
Some JDK Collection type Signatures (PECS at work!)
public interface Collection<E> extends Iterable<E> /* Java 8 */
/* ArrayList */
/*use-site covariance */
void addAll(Collection<? extends E> items);
KOTLIN
Some Kotlin Collection type Signatures (CIPO at work!)
/*declaration-site covariance */
public interface Collection<out E> : Iterable<E> /*covariant as to be expected */
/* MutableList */
public fun addAll(elements: Collection<E>): Boolean
Note: Generics are invariant in both Java and Kotlin, RAW types not implemented (no backwards compatibility needed)
Reified Generics
We can!
inline fun <reified T> f(a: Any) {
if (a is T) println("type matches!")
println("Hey! my class is ${T::class.java}!")
}
f<Int>(123)
We can’t!
inline fun <reified T> f() {
val x = T() /* T t = new T() */
}
f<SomeType>()
We can’t!
fun <T> g(a: Any) {
f<T>(a) // error, cant pass T
}
inline fun <reified T> f(a: Any) {}
We can!
inline fun <reified T> g(a: Any) {
f<T>(a) // OK, reified!
}
inline fun <reified T> f(a: Any) {}
These are not callable from Java code!
Collection in Kotlin R/O != immutable
Can’t be modified through the Collection<T>, Set<T> and List<T> interface methods (no mutable
methods exist on those interfaces!)
Some extension functions cast to a mutable type from an immutable implementation (according to JB)
and therefore Collection may be non thread-safe!
Using ‘as’ to cast to a MutableList succeeds but using a mutate operation (such as add) causes a
java.lang.UnsupportedOperationException
Functional Aspects
A Tour of Kotlin
8
Streams-Like Processing (similar to streams in Java 8)
data class Student(
val name: String,
val surname: String,
val passing: Boolean,
val averageGrade: Double
)
studentz
.asSequence()
.filter { it.passing && it.averageGrade > 80.0 }
.sortedBy { it.averageGrade }
.take(2)
//.toList()
//.forEach { println(it.name) }
.forEach(::println)
Trivial example data filtering and extraction
val studentz = arrayListOf<Studentz>(
Studentz("edik","austin",false,70.0),
Studentz("lenka","penka",true,85.0),
Studentz("lasma","plasma",true,100.0),
Studentz("volodya","polodya",true,98.0)
)
Note: Can be viewed as a one-time
iterator on a Collection
Lambda Trivial HoF/Lambda example
//HOF
fun myHigherOrderFun(s:ArrayList<Studentz>,
f: (ArrayList<Studentz>)-> Pair<String,String> ) = f(s)
//ext fun on Sequence
fun <T> Sequence<T>.sequenceToTopNamePair() = this
.toList()
.let { Pair(it[0].toString(), it[1].toString()) }
//Lambda
myHigherOrderFun (studentz) {
it.asSequence() //make it lazy
.filter { it.passing && it.averageGrade > 80.0 }
.sortedByDescending { it.averageGrade }
.take(2)
.sequenceToTopNamePair()}
.let(::println)
Lambda
//SAM
Thread(Runnable {
/* actions */
}).start()
val r = Runnable { /* actions */ }
runz(r)
fun runz(f: (Runnable)) = Thread(f).start()
//Kotlin, no need for use-site variance, declaration-site has this!
fun <T : Comparable<in T>> dooz(a:T, b:T, p: (T,T) -> Boolean ) = p(a,b)
val p = { a: Number, b: Number -> (a > b) }
dooz(3.4f, 2.4f, p).let { println(it) } //true
val p2 = { a:Char, b:Char -> (a > b) }
dooz('a', 'b', p2).let(::println) //false
//another (completely different) way
fun <T:Comparable<in T>> T.dooz(b:T) = this > b
"a".dooz("b")
3.4f.dooz(2.4f)
studentz5.dooz(studentz4)
Concurrency
Co-routines
8
“ Kotlin deliberately has no
constructs for concurrency built
into the language. We believe
this should be handled by
libraries.
--Andrey Breslav
Kotlin Language Creator
Thanks!
Any questions?
You can find me ed@ryer.ru
Ad

More Related Content

What's hot (20)

Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008
Yardena Meymann
 
Java Basics
Java BasicsJava Basics
Java Basics
sunilsahu07
 
Mirah Talk for Boulder Ruby Group
Mirah Talk for Boulder Ruby GroupMirah Talk for Boulder Ruby Group
Mirah Talk for Boulder Ruby Group
baroquebobcat
 
Kotlin for Android Developers - 3
Kotlin for Android Developers - 3Kotlin for Android Developers - 3
Kotlin for Android Developers - 3
Mohamed Nabil, MSc.
 
Kotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsKotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projects
Bartosz Kosarzycki
 
Core java Essentials
Core java EssentialsCore java Essentials
Core java Essentials
SRM Institute of Science & Technology, Tiruchirappalli
 
A topology of memory leaks on the JVM
A topology of memory leaks on the JVMA topology of memory leaks on the JVM
A topology of memory leaks on the JVM
Rafael Winterhalter
 
Seeking Clojure
Seeking ClojureSeeking Clojure
Seeking Clojure
chrisriceuk
 
Develop your next app with kotlin @ AndroidMakersFr 2017
Develop your next app with kotlin @ AndroidMakersFr 2017Develop your next app with kotlin @ AndroidMakersFr 2017
Develop your next app with kotlin @ AndroidMakersFr 2017
Arnaud Giuliani
 
Scala introduction
Scala introductionScala introduction
Scala introduction
Yardena Meymann
 
Presentation to java
Presentation  to  javaPresentation  to  java
Presentation to java
Ganesh Chittalwar
 
Java Concurrency by Example
Java Concurrency by ExampleJava Concurrency by Example
Java Concurrency by Example
Ganesh Samarthyam
 
All about scala
All about scalaAll about scala
All about scala
Yardena Meymann
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
Lorna Mitchell
 
Java
JavaJava
Java
s4al_com
 
Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)
HamletDRC
 
Ceylon - the language and its tools
Ceylon - the language and its toolsCeylon - the language and its tools
Ceylon - the language and its tools
Max Andersen
 
Effective kotlin
Effective kotlinEffective kotlin
Effective kotlin
AndrewOrobator
 
Hello java
Hello java  Hello java
Hello java
University of Babylon
 
Hello Java-First Level
Hello Java-First LevelHello Java-First Level
Hello Java-First Level
Dr. Raaid Alubady
 

Similar to Kotlin Language Features - A Java comparison (20)

What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)
Pavlo Baron
 
Introduction to kotlin
Introduction to kotlinIntroduction to kotlin
Introduction to kotlin
Shaul Rosenzwieg
 
Intro to Kotlin
Intro to KotlinIntro to Kotlin
Intro to Kotlin
Magda Miu
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
Abbas Raza
 
Game Design and Development Workshop Day 1
Game Design and Development Workshop Day 1Game Design and Development Workshop Day 1
Game Design and Development Workshop Day 1
Troy Miles
 
Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017
Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017 Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017
Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017
Codemotion
 
Polyglot Programming @ Jax.de 2010
Polyglot Programming @ Jax.de 2010Polyglot Programming @ Jax.de 2010
Polyglot Programming @ Jax.de 2010
Andres Almiray
 
Intro to Java for C++ Developers
Intro to Java for C++ DevelopersIntro to Java for C++ Developers
Intro to Java for C++ Developers
Zachary Blair
 
Intro to scala
Intro to scalaIntro to scala
Intro to scala
Joe Zulli
 
Polyglot Programming in the JVM
Polyglot Programming in the JVMPolyglot Programming in the JVM
Polyglot Programming in the JVM
Andres Almiray
 
Android & Kotlin - The code awakens #03
Android & Kotlin - The code awakens #03Android & Kotlin - The code awakens #03
Android & Kotlin - The code awakens #03
Omar Miatello
 
Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation Stal
Michael Stal
 
Meetup di GDG Italia - Leonardo Pirro - Codemotion Rome 2018
Meetup di GDG Italia - Leonardo Pirro -  Codemotion Rome 2018 Meetup di GDG Italia - Leonardo Pirro -  Codemotion Rome 2018
Meetup di GDG Italia - Leonardo Pirro - Codemotion Rome 2018
Codemotion
 
Scala ntnu
Scala ntnuScala ntnu
Scala ntnu
Alf Kristian Støyle
 
Kotlin for Android devs
Kotlin for Android devsKotlin for Android devs
Kotlin for Android devs
Adit Lal
 
Scala - brief intro
Scala - brief introScala - brief intro
Scala - brief intro
Razvan Cojocaru
 
Kotlin for Android Development
Kotlin for Android DevelopmentKotlin for Android Development
Kotlin for Android Development
Speck&Tech
 
Scala Intro
Scala IntroScala Intro
Scala Intro
Alexey (Mr_Mig) Migutsky
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
Loïc Descotte
 
2022 May - Shoulders of Giants - Amsterdam - Kotlin Dev Day.pdf
2022 May - Shoulders of Giants - Amsterdam - Kotlin Dev Day.pdf2022 May - Shoulders of Giants - Amsterdam - Kotlin Dev Day.pdf
2022 May - Shoulders of Giants - Amsterdam - Kotlin Dev Day.pdf
Andrey Breslav
 
What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)
Pavlo Baron
 
Intro to Kotlin
Intro to KotlinIntro to Kotlin
Intro to Kotlin
Magda Miu
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
Abbas Raza
 
Game Design and Development Workshop Day 1
Game Design and Development Workshop Day 1Game Design and Development Workshop Day 1
Game Design and Development Workshop Day 1
Troy Miles
 
Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017
Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017 Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017
Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017
Codemotion
 
Polyglot Programming @ Jax.de 2010
Polyglot Programming @ Jax.de 2010Polyglot Programming @ Jax.de 2010
Polyglot Programming @ Jax.de 2010
Andres Almiray
 
Intro to Java for C++ Developers
Intro to Java for C++ DevelopersIntro to Java for C++ Developers
Intro to Java for C++ Developers
Zachary Blair
 
Intro to scala
Intro to scalaIntro to scala
Intro to scala
Joe Zulli
 
Polyglot Programming in the JVM
Polyglot Programming in the JVMPolyglot Programming in the JVM
Polyglot Programming in the JVM
Andres Almiray
 
Android & Kotlin - The code awakens #03
Android & Kotlin - The code awakens #03Android & Kotlin - The code awakens #03
Android & Kotlin - The code awakens #03
Omar Miatello
 
Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation Stal
Michael Stal
 
Meetup di GDG Italia - Leonardo Pirro - Codemotion Rome 2018
Meetup di GDG Italia - Leonardo Pirro -  Codemotion Rome 2018 Meetup di GDG Italia - Leonardo Pirro -  Codemotion Rome 2018
Meetup di GDG Italia - Leonardo Pirro - Codemotion Rome 2018
Codemotion
 
Kotlin for Android devs
Kotlin for Android devsKotlin for Android devs
Kotlin for Android devs
Adit Lal
 
Kotlin for Android Development
Kotlin for Android DevelopmentKotlin for Android Development
Kotlin for Android Development
Speck&Tech
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
Loïc Descotte
 
2022 May - Shoulders of Giants - Amsterdam - Kotlin Dev Day.pdf
2022 May - Shoulders of Giants - Amsterdam - Kotlin Dev Day.pdf2022 May - Shoulders of Giants - Amsterdam - Kotlin Dev Day.pdf
2022 May - Shoulders of Giants - Amsterdam - Kotlin Dev Day.pdf
Andrey Breslav
 
Ad

Recently uploaded (20)

GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
James Anderson
 
UiPath Automation Suite – Cas d'usage d'une NGO internationale basée à Genève
UiPath Automation Suite – Cas d'usage d'une NGO internationale basée à GenèveUiPath Automation Suite – Cas d'usage d'une NGO internationale basée à Genève
UiPath Automation Suite – Cas d'usage d'une NGO internationale basée à Genève
UiPathCommunity
 
How to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabberHow to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabber
eGrabber
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
IT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information TechnologyIT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information Technology
SHEHABALYAMANI
 
fennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solutionfennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solution
shallal2
 
machines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdfmachines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdf
AmirStern2
 
Q1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor PresentationQ1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor Presentation
Dropbox
 
UiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer OpportunitiesUiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer Opportunities
DianaGray10
 
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Raffi Khatchadourian
 
Viam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdfViam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdf
camilalamoratta
 
Does Pornify Allow NSFW? Everything You Should Know
Does Pornify Allow NSFW? Everything You Should KnowDoes Pornify Allow NSFW? Everything You Should Know
Does Pornify Allow NSFW? Everything You Should Know
Pornify CC
 
Cybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and MitigationCybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and Mitigation
VICTOR MAESTRE RAMIREZ
 
Slack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teamsSlack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teams
Nacho Cougil
 
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz
 
Agentic Automation - Delhi UiPath Community Meetup
Agentic Automation - Delhi UiPath Community MeetupAgentic Automation - Delhi UiPath Community Meetup
Agentic Automation - Delhi UiPath Community Meetup
Manoj Batra (1600 + Connections)
 
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdfKit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Wonjun Hwang
 
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Safe Software
 
AI You Can Trust: The Critical Role of Governance and Quality.pdf
AI You Can Trust: The Critical Role of Governance and Quality.pdfAI You Can Trust: The Critical Role of Governance and Quality.pdf
AI You Can Trust: The Critical Role of Governance and Quality.pdf
Precisely
 
Build With AI - In Person Session Slides.pdf
Build With AI - In Person Session Slides.pdfBuild With AI - In Person Session Slides.pdf
Build With AI - In Person Session Slides.pdf
Google Developer Group - Harare
 
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
James Anderson
 
UiPath Automation Suite – Cas d'usage d'une NGO internationale basée à Genève
UiPath Automation Suite – Cas d'usage d'une NGO internationale basée à GenèveUiPath Automation Suite – Cas d'usage d'une NGO internationale basée à Genève
UiPath Automation Suite – Cas d'usage d'une NGO internationale basée à Genève
UiPathCommunity
 
How to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabberHow to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabber
eGrabber
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
IT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information TechnologyIT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information Technology
SHEHABALYAMANI
 
fennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solutionfennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solution
shallal2
 
machines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdfmachines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdf
AmirStern2
 
Q1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor PresentationQ1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor Presentation
Dropbox
 
UiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer OpportunitiesUiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer Opportunities
DianaGray10
 
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Raffi Khatchadourian
 
Viam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdfViam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdf
camilalamoratta
 
Does Pornify Allow NSFW? Everything You Should Know
Does Pornify Allow NSFW? Everything You Should KnowDoes Pornify Allow NSFW? Everything You Should Know
Does Pornify Allow NSFW? Everything You Should Know
Pornify CC
 
Cybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and MitigationCybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and Mitigation
VICTOR MAESTRE RAMIREZ
 
Slack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teamsSlack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teams
Nacho Cougil
 
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz
 
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdfKit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Wonjun Hwang
 
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Safe Software
 
AI You Can Trust: The Critical Role of Governance and Quality.pdf
AI You Can Trust: The Critical Role of Governance and Quality.pdfAI You Can Trust: The Critical Role of Governance and Quality.pdf
AI You Can Trust: The Critical Role of Governance and Quality.pdf
Precisely
 
Ad

Kotlin Language Features - A Java comparison

  • 1. A Brief Language Overview Ed Austin ed@ryer.ru
  • 2. Hello! Привет! Developer ed@ryer.ru +44 7726 05 0000 +7 925 871 9411 I am Ed Austin Я Эд Остин Презентация будет на английском
  • 3. Disclaimer! Liable to have mistakes! Don’t hesitate to point them out!
  • 4. Part 1 Some Background What is Kotlin and how does it differ to Java? 1
  • 5. What is in the Name? Named after an Island off the coast of St. Petersburg* *The second largest city in the Russian Federation and the home of JetBrains © Ed Austin 2017
  • 6. History of Kotlin? 2010: JetBrains started the Kotlin Project 2011: Public Announcement 2012: Open Sourced under Apache 2 2016: Release 1.0 2017: Release 1.1 Google announced first class language support 2018: Release 1.2 Release 2.0?? No indication but probably 2019+ ._X = bug fixes e.g. 1.02 _.X = incremental, no major changes e.g. 1.1, 1.2 X.0 = major (may introduce major features, deprecate others) e.g. 2.0
  • 7. What exactly is Kotlin? Java-like with Clean abstractions, concise syntax, and type safety. Full Java interoperability, significant tool chain from the start. Fully open-source (Apache 2). Sugared Java Java++ Java on Steroids Scala for idiots The "PL/1" of our times 💣 Depending on your point of view the Language can be described in various ways! Negative View Positive View © Ed Austin 2017
  • 8. What/Who influenced Kotlin? 😉 Disclaimer: I did ask Andrey Breslav (Language Creator at JB) Josh Bloch Grady Booch Kent Beck Erich Gamma Martin Fowler Groovy, Java, Scala… Effective Java OOD TDD + Smalltalk stuff GoF OOD/UML Smalltalk, Algol 60, Simula 67
  • 9. Language Viewpoint Imperative? Functional? Object Oriented? From a purists point of view no, but then again neither are Java or C++ So from a language classification viewpoint it can be viewed in much the same way as Java. © Ed Austin 2017
  • 10. Supported Architectures Kompiler + Tool support for: Android (ART) – Google Official Support Likely to become the dominant mobile applications language for AOSP within a few years JVM 1.6+ (later JVM may support better optimizations, not yet Valhalla EAB) Native (x86/ARM/Others) Transpiles to JS Basically in the long-term to be running on as many platforms as possible © Ed Austin 2017
  • 11. What differs from Java Non-nullable Type System All types are non-nullable by default (but can and may have to be nullable in some circumstances) No Covariant Arrays Covariant arrays are gone, now invariant. No Primitives Everything in Kotlin is an Object. No Checked Exceptions Don’t exist in the class hierarchy of Errors. No Statics No statics exist in the language, at least conceptually, but likely to change No Raw types No need for backward JVM compatibility, we also have reification! AND Top level properties and funs, Data Classes, Co-routines, Smartcasting, Operator Overloading, Properties, Extension Functions, Companion Objects, Mutable/Immutable collections, Singletons, String templates, Range expressions, Delegation, Generics … NO SEMICOLONS!!!
  • 12. Some of what it doesn’t have… Multi-Catch For Exception handling True immutability Same implementation as Java reference types Value Types Beyond simple JVM types JVM limitation? PP Visibility/Namespaces For use in Java interoperability. Array Slices data[a:b], data[:b] etc. SAM Conversion Only for Java not for Kotlin interfaces No Boolean Op Overloading All other operators are however overloadable
  • 13. Classes, Objects & Inheritance Basics 2
  • 14. Class Hierarchy - Any and Nothing Any Equivalent to java.lang.Object* Nothing Is not Void! (Unit type is Void) Note:Technically Object is actually a subtype of Any (TBD) Diagram courtesy of JetBrains
  • 15. Interface and Implementation Inheritance private interface IfMyInterface : IfSomeOtherInterface{ val x get() = 100 fun someMethoda(x: Any?) : Unit /* contract unfulfilled */ fun someMethodb(x: Int) : Int = 123 /* Java 8’ish */ } open class SomeClass /* open for extension – final by default! */ abstract class SomeAbstractClass : Any() final class MyClass : SomeClass(), IfMyInterface { override fun someMethoda(x: Any?) { /* body */ } } class SubClass : SomeAbstractClass() { /* implement the abstract class method bodies */ } Note: Syntax is very close to Java and readable by any Java developer.
  • 16. Class Basics //constructor keyword optional //default parameters public class TopLevelClass private constructor(val name:String="unknown", val age:Int =-1) { var t = 10 //secondary C constructor(name:String, age:Int, gender:String ="unknown") : this(name, age) {} //tripe, just to show local instantiation inner class InnerClass(){ val innert = t val tlc = TopLevelClass("jane") } class AClass{ val nestedt = t fun showme() = 123 } } /* … */ /* call with a named parameter, and using default parameters */ val tlc = TopLevelClass("john", 22, gender="male") //call 3-arg C
  • 17. Sealed Class abstract sealed class Sealy { /* default private C */ class Red : Sealy() class Green : Sealy() } /* … */ val sealy : Sealy = Sealy.Red() val output = when (sealy) { is Sealy.Red -> "red" is Sealy.Green -> "green" /* no else branch! */ } println("result is ${output}") //shortened println syntax and String templates Primary benefit: Exhaustive case when expression. Specified set of specific values (such as a state machine). Similar to an enum class, effectively abstract so not instantiable.
  • 18. Data Class data class Student(val name: String, var age: Int) This is the entire class (no body)! getters and if applicable setters autogenerated hashcode/equals pair for the object (structural equivalence) Custom toString (in the form of (name=John, age=30) ) Restriction: Can’t be opened for extension, abstract, sealed or inner. Copy extension function (with optional modification) val stu = Student("ed", 42) val stu_amended_copy = stu.copy(age=43) //not referential!
  • 19. Kotlin Singleton via object © Ed Austin 2017 // Java public class SingletonDcl { private static SingletonDcl instance = null; private SingletonDcl(){ } private synchronized static void createInstance() { if (instance == null) { instance = new SingletonDcl(); } } public static SingletonDcl getInstance() { if (instance == null) createInstance(); return instance; } public void foo() {} } SingletonDcl.getInstance().foo(); //Kotlin Thread-safe version public object Singleton { public fun foo() {} } Singleton.foo()
  • 20. Anonymous Class var clickCount = 0 window.addMouseListener(object : MouseAdapter() { override fun mouseClicked(e: MouseEvent) { // ... clickCount++ //true closure! } override fun mouseEntered(e: MouseEvent) { // ... } }) Unlike older versions of Java…
  • 21. Companion Objects Quite possible at 2.0 will be deprecated in favour of static objects Possibly better to write top level functions and variables before the class rather than use a Companion Object- elizarov JetBrains Team Aug ’17 (most of the time.. No @JvmStatic) public class Penka { val id: Int? = null init { id = nextId?.inc() } private companion object : SomeClass var nextId : Int? = 1 fun masha() {} } } fun main(args: Array<String>) { Penka() println(Penka().id!!) } //can inherit! public class Lenka { Integer id; { id = nextId++; } private static Integer nextId = 1; static void masha{} public static void main(String[] args) { new Lenka(); System.out.println(new Lenka().id); } } var nextId : Int? = 1 fun masha() {} //above package namespace public class Penka { val id: Int? = null init { id = nextId?.inc() } private companion object { var nextId : Int? = 1 fun masha() {} } } fun main(args: Array<String>) { Penka() println(Penka().id!!) }
  • 22. Companion Objects Simply put a companion object is simply a final static method! File: MyTest.kt package foo class Penka { private companion object { fun masha() { /* some code */ } } } public final class MyTestKt { public static final void masha() { /* our code */ } }
  • 23. Top Level Properties and Functions We can have properties and methods at Top level These are compiled down to JVM static methods within YourPackageName.class (delegates implementations to the so-named package- parts) Diagram courtesy of JetBrains
  • 25. Smart Casts fun demo(x: Any) { if (x is String) { print(x.length) // x is automatically cast to String } } if (x !is String) return print(x.length) // x is automatically cast to String Safe “as?” /Unsafe “as” Cast val x: String? = y as? String //safe cast.. return null if cast not possible val x: String = y as String //unsafe cast, may throw CCE!
  • 26. Extension Functions (add a method to any class) Extension fun: fun Int.appendString(s:String) = this.toString() + s 123.appendString("abc") 123abc infix fun Int.appendString(s:String) = this.toString() + s 123 appendString "abc" 123abc EBNF: [Infix] fun MyClassTobeExtended.myNoneOverridenMethod([arg]) [: returnType ] = expression | [{ /* block */ }] In the result refer to the first argument as the receiver with this, and the arg as the parameter sent with the message Likely a better way to do this by overloading : operator fun Int.plus(s:String) = this.toString() + s 123 + "abc" 123abc Java.lang.Integer For obvious reasons doesn’t have an appendString method* *I stand slightly corrected, "+“ is overloaded as to: String s = 123 + "abc“ which makes this example pretty useless Note: We can’t override existing methods
  • 27. Standard Functions on Any (let, apply, run) public inline fun <T, R> T.run(block: T.() -> R): R = block() myClassInstance.run { //we could also use a property and apply a method such as length this.myVar = 123 this.funkyFunc(123) } //returns the result of the block (last arg) and mutates the receiver public inline fun <T> T.apply(block: T.() -> Unit): T { block(); return this } val person = myPersonInstance.apply { this.name = "lenka penka" this.age = 33 } //Calls the specified function [block] with "this" value as its receiver and returns "this" value. //mutates receiver public inline fun <T, R> T.let(block: (T) -> R): R = block(this) "something here".let {println("The length of this String is ${it.length}") } //Calls the specified function [block] with "this" value as its argument and returns its result.
  • 28. Standard Functions on Any (let, apply, run) if (listz.run { this.add(11) this.add(12) } //mutates receiver and returns the last result (true) ){ listz.apply { this.remove(1) } //mutate and return receiver .let { println(it) } //pass and print the receiver (implicit it) } [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] val listz = arrayListOf<Int>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
  • 29. Standard Functions on Any (Let, Apply. Run)
  • 31. Visibility Modifiers Classes, objects, interfaces, constructors, functions, properties and their setters can have visibility modifiers Top Level Functions, properties and classes, objects and interfaces public by default private “file level” visibility internal same module (i.e. PP) Class Level Members declared within a Class public by default private for scope inside class (and its members*) internal same module (i.e. PP) protected class + subclasses *Outer cannot see private members of its inner classes, nor protected, although you can extend an open inner class within the original class to gain access to the protected members!
  • 32. Access Modifiers Classes are… final by default open can be extended package foo open class MyClass class bar : MyClass() final class MyClass class foo : MyClass() By default classes cannot be extended (are final), this includes inner classes
  • 33. Visibility/Access Modifiers Example // file name: Example.kt package foo /* top level! */ private fun foo() {} // visible inside example.kt public var bar: Int = 5 // property is visible everywhere private set // setter is visible only in example.kt internal val baz = 6 // visible inside foo open class MyClass(){ //not default private final inner class Inner(){} //reference to outer class public final class Nested(){} //no refs to outer class public final object Objy{} //effectively static/singleton }
  • 35. Java -> Kotlin Primitive/Wrapped Mapping
  • 36. Kotlin Properties Abstraction over fields Can be seen as a “front end” for any backing field Allows custom getters/setters for variables We can use expressions on evaluation Transparent to developers (get default implementation) Many developers may never use them (using the hidden default implementation) Allows Custom access We can add specific visibility modifiers such as public for get and private for set © Ed Austin 2017 These are the equivalent of Java fields
  • 37. Kotlin Properties [<modifiers>] var|val <propertyName>[: <PropertyType>] [= <property_initializer>] [<getter>] [<setter>] var someProperty: String = "defaultValue“ val x get() = field get() = z + 100 private set(value) { field = value } © Ed Austin 2017 We can modify the getter/setter of the property with an expression evaluated on set/get as well as visibility modifiers. A val doesn’t obviously have a setter as it can’t mutate! Wait a minute….. val someProperty2: String = "defaultValue" public get() = field + random.nextInt()
  • 38. Kotlin Example Java Mapping class Student(val name: String, uni:String) fun main(args: Array<String>) { val a = 123 val b = 444f val c = "ABC" val d = ("[A..Z]").toRegex() val e = 1..100 val f = Student("Masha", “MGU") val g = mutableListOf<Student>() val h = hashMapOf<Int,Student>() val i = arrayOf(a,b,c,d,e,f,g,h) i.forEach { println(it.javaClass.name) } } Types are mostly inferred by the Type system, and need not be specified, most underlying types are from the JDK, the few from Kotlin are additional types
  • 39. Managing Null Pointers Design goal was to eliminate NPE as far as possible Allegedly NPE was/is a huge issue for Java Developers. Type system rigorously enforces non-nullability Heavy static typing makes sure you don’t get into trouble All types have a nullable alternative We can specify a null type if we really wish Nullability was not entirely eliminated Any interoperability with Java brings with it the nullability issue © Ed Austin 2017
  • 40. Nullable Type System Biggest Kotlin difference: A revised Type System that enforces non-nullability by default var x = 100f /* non-nullable */ var y : Int? = 100 /* this is nullable */ var g : MutableList<Student>? = mutableListOf<Student>() We simply add “?” After a type definition to indicate the type is nullable, we must explicitly state (most) nullable types. IntelliJ picks up static semantic abuse issues and forces use of various operators to avoid R/T NPE.
  • 41. Comparing Types Referential Equality === (negated !==) Points to same object i.e. shallow Value/Structural Equality == (negated !=) Points to same data i.e. deep a?.equals(b) ?: (b === null) If a not null call equals(b:Any?) and return result otherwise if a is null (using elvis) return b===null (referential) i.e. null == null (true). Or null = not null (false) The above are used to provide safety working with nullable types in a type system with mixed typing No "==" or ". equals()" confusion Java == Java .equals
  • 42. Some Type System Operators Safe Call Operator ?. Allows a check for receiver nullability before allowing access to a method else return null if the receiver is null Elvis Operator ?: Return a non-null expression on the left else return a value on the right Double Bang Operator !! Converts any value to a non-null type and throws an NPE if null Example: var foo : String? = "bar" /* … */ val len = foo?.length() Example: var foo: String? = "bar" /* … */ val len = (foo?.length) ?: -1 Example var foo: String? = "bar" /* … */ val len = foo!!.length The above are used to provide safety working with nullable types in a type system with mixed typing Safe Cast as? Cast returns null and not CCE if the cast fails Example: val aInt: Int? = a as? Int
  • 43. Nullable Types Woes #1 i++ Java i++ Kotlin Non-nullable i?.++ (you think?) i?.inc() (my second guess) wtf? No side-effects! Receiver doesn’t mutate! Kotlin Nullable
  • 44. Types Woes #2 Thankyou Ilya Ryzhenkov from JetBrains Kotlin Libraries Team for the overridden operator code. operator fun Int?.inc() : Int? = this?.inc() fun test() { var i: Int? = null i++ } i++ (for nullables)  i = if (i == null) null else i.inc() Nullable
  • 46. Some function Signatures Java Kotlin String jrasl() { return “abc” } fun justReturnAStringLiteral : String = “abc” <T> void f(T a, Object b) fun <T> f(a:T, b:Any?) : Unit <E> void a(Collection<? extends E> items) fun <E> a(items: Collection<out E>) : Unit void checkConnection() throws IOException No equivalent No equivalent fun willNeverReturn(): Nothing { throw IOException() }
  • 47. Imperative Structures Java Kotlin (can be expression) for(int i=0;i<=10;i++) for(i in 0..10) //i implicit decl/local scope while (a>0) {/*..*/} while (a>0) {/*..*/} do { block } while (condition) do { block } while (condition) String res; val res = if (x > 10) "foo" else "bar" if (x>10) res="foo"; else res="bar"; if ((x>0) && (x<100)) if (x in 1..100) for (MyType s : collection) for (item in collection) switch - case when {...} / when(..) {...} etc
  • 48. Communicating with Java A Tour of Kotlin 6
  • 49. (Nullable) Platform Types Java composite types have potential nullable references! That is all reference types in the language (anything other than the basic value types) When calling to Java we need to prepare for this! Because we most often are passing back something that could cause us some issues These types are called “Platform Types” in Kotlin They are a special type and can cause us NPE when dereferenced because they are nullable We can use Java-side annotations to pass information Such as whether to treat the passed reference as @nullable etc.
  • 50. Platform Types (from Java to Kotlin) fun main(args: Array<String>) { val mjt = MyJavaTest() //what is the dereference result? println(mjt.getBaddy()) } public class MyJavaTest{ private Integer baddy; /* … */ public Integer getBaddy(){ return baddy; } } What happens if we call the Java getter from Kotlin? Java Kotlin © Ed Austin 2017
  • 51. Platform Types (from Java to Kotlin) fun main(args: Array<String>) { val mjt = MyJavaTest() //what is the dereference result? println(mjt.getBaddy()?:"invalid state!") } public class MyJavaTest{ private Integer baddy; /* … */ public Integer getBaddy(){ return baddy; } } What happens if we call the Kotlin code from Java? Java Kotlin © Ed Austin 2017
  • 52. From Kotlin to Java Kotlin //@JvmOverloads allows overloaded Constructors/funs from Java class TopLevelClazz @JvmOverloads constructor( val name:String="unknown", val age:Int =-1, val gender:String="unknown") {} Java TopLevelClazz tlc1 = new TopLevelClazz("ed"); System.out.println(tlc1.getAge()); //auto-generated getter from Kotlin TopLevelClazz tlc2 = new TopLevelClazz("ed",43); TopLevelClazz tlc3 = new TopLevelClazz("ed",43,"male");
  • 53. Types (from Kotlin to Java) import penka.Lenka; class Penka { /* … */ new Lenka().name; //@JvmStatic //Instance Lenka.foo(); Lenka.MyExposed.foo(); Lenka.bar(); Lenka.MyExposed.bar(); w/o name: Lenka.Companion. … package penka final class Lenka { @JvmField private var name = "hello world“ private set companion object MyExposed { @JvmStatic fun foo() {} fun bar() {} } } Java VisibilityKotlin © Ed Austin 2017 @JvmStatic will create a proxy static class (so seen as a static class from Java) that calls the Kotlin object singleton instance.
  • 54. Kotlin Exceptions //Kotlin side @Throws(IOException::class) fun foo() : Nothing { throw IOException() } No mandatory handing in Kotlin but We can if we wish same as Java with try- catch //Java side does the call, becomes checked and forces handling try { MyKotlinTest().foo(); } catch (IOException e) { //…// } finally { //…// }
  • 56. Collection/Generics in Kotlin Collection has Mutable and immutable interfaces that branches off into concrete implementations. Map has a separate interface (like JDK MutableMap extending Map) Generics are split into covariant/invariant/contravariant signatures using the out and in keywords at declaration-site. No more extends/super spaghetti! (Use where for multiple constraints). Uses the JDK concrete implementations under the hood: @SinceKotlin("1.1") @kotlin.internal.InlineOnly public inline fun <T> mutableListOf(): MutableList<T> = ArrayList() @SinceKotlin("1.1") public typealias ArrayList<E> = java.util.ArrayList<E>
  • 58. Generics Conversion Invariant Kotlin: <T> Java: <T> Example: Collection concrete classes are invariant Covariant (Producer) Kotlin: <out E> Java: <? Extends E> Upper bounded Example: Collection interfaces are covariant Contravariant (Consumer) Kotlin: <in E> Java: <? Super E> Lower bounded Example: Comparable interface is contravariant (on declaration- site in Kotlin) Remember PECS = CIPO Wildcard <*> <out Any?> <?> <? extends Object> Wildcard example: iterate/read through a list of any type Note: No wildcards in Kotlin Generics!
  • 59. Generics Conversion Java: //JSDK: public interface Comparable <T> //Effective Java it. 28 recommends //<T extends Comparable<? super T>> //<T extends Comparable<T>> <T extends Comparable<? super T>> void sortz(List<T> x){ sort(x); } Kotlin: //KSDK: public interface Comparable<in T> fun <T:Comparable<in T>> sortz(x :List<T>){ sort(x) } Remember PECS = CIPO Kotlin: fun processListz(l : MutableList<*>) { l.add(22) for(item in l) println(item) } val l = mutableListOf<Any>(123, "abc") processListz(l) Java: void processListz(List<?> l) { l.add(22) for(Object item : l) System.out.println(item); } /* ... */ List<Object> l = new ArrayList<Object>(); l.add(123); l.add("abc"); processListz(l); *Warning:(16, 25) Kotlin: Projection is redundant: the corresponding type parameter of Comparable has the same variance
  • 60. Collection Generics at Declaration-site JAVA Some JDK Collection type Signatures (PECS at work!) public interface Collection<E> extends Iterable<E> /* Java 8 */ /* ArrayList */ /*use-site covariance */ void addAll(Collection<? extends E> items); KOTLIN Some Kotlin Collection type Signatures (CIPO at work!) /*declaration-site covariance */ public interface Collection<out E> : Iterable<E> /*covariant as to be expected */ /* MutableList */ public fun addAll(elements: Collection<E>): Boolean Note: Generics are invariant in both Java and Kotlin, RAW types not implemented (no backwards compatibility needed)
  • 61. Reified Generics We can! inline fun <reified T> f(a: Any) { if (a is T) println("type matches!") println("Hey! my class is ${T::class.java}!") } f<Int>(123) We can’t! inline fun <reified T> f() { val x = T() /* T t = new T() */ } f<SomeType>() We can’t! fun <T> g(a: Any) { f<T>(a) // error, cant pass T } inline fun <reified T> f(a: Any) {} We can! inline fun <reified T> g(a: Any) { f<T>(a) // OK, reified! } inline fun <reified T> f(a: Any) {} These are not callable from Java code!
  • 62. Collection in Kotlin R/O != immutable Can’t be modified through the Collection<T>, Set<T> and List<T> interface methods (no mutable methods exist on those interfaces!) Some extension functions cast to a mutable type from an immutable implementation (according to JB) and therefore Collection may be non thread-safe! Using ‘as’ to cast to a MutableList succeeds but using a mutate operation (such as add) causes a java.lang.UnsupportedOperationException
  • 64. Streams-Like Processing (similar to streams in Java 8) data class Student( val name: String, val surname: String, val passing: Boolean, val averageGrade: Double ) studentz .asSequence() .filter { it.passing && it.averageGrade > 80.0 } .sortedBy { it.averageGrade } .take(2) //.toList() //.forEach { println(it.name) } .forEach(::println) Trivial example data filtering and extraction val studentz = arrayListOf<Studentz>( Studentz("edik","austin",false,70.0), Studentz("lenka","penka",true,85.0), Studentz("lasma","plasma",true,100.0), Studentz("volodya","polodya",true,98.0) ) Note: Can be viewed as a one-time iterator on a Collection
  • 65. Lambda Trivial HoF/Lambda example //HOF fun myHigherOrderFun(s:ArrayList<Studentz>, f: (ArrayList<Studentz>)-> Pair<String,String> ) = f(s) //ext fun on Sequence fun <T> Sequence<T>.sequenceToTopNamePair() = this .toList() .let { Pair(it[0].toString(), it[1].toString()) } //Lambda myHigherOrderFun (studentz) { it.asSequence() //make it lazy .filter { it.passing && it.averageGrade > 80.0 } .sortedByDescending { it.averageGrade } .take(2) .sequenceToTopNamePair()} .let(::println)
  • 66. Lambda //SAM Thread(Runnable { /* actions */ }).start() val r = Runnable { /* actions */ } runz(r) fun runz(f: (Runnable)) = Thread(f).start() //Kotlin, no need for use-site variance, declaration-site has this! fun <T : Comparable<in T>> dooz(a:T, b:T, p: (T,T) -> Boolean ) = p(a,b) val p = { a: Number, b: Number -> (a > b) } dooz(3.4f, 2.4f, p).let { println(it) } //true val p2 = { a:Char, b:Char -> (a > b) } dooz('a', 'b', p2).let(::println) //false //another (completely different) way fun <T:Comparable<in T>> T.dooz(b:T) = this > b "a".dooz("b") 3.4f.dooz(2.4f) studentz5.dooz(studentz4)
  • 68. “ Kotlin deliberately has no constructs for concurrency built into the language. We believe this should be handled by libraries. --Andrey Breslav Kotlin Language Creator
  • 69. Thanks! Any questions? You can find me ed@ryer.ru

Editor's Notes

  • #6: Been everywhere in RF except!!
  • #8: Negative – Middling - JetBrains view… Martin Odersky
  • #9: The usual suspects…
  • #10: A PIE OM
  • #13: C-Typ/I-Type etc, L-Type Objects, Q-Type Value types, @DeriveValueType
  • #15: Objects methods HET, no NWF, no unboxed void
  • #16: Interface are psf like java! No con abstract obv.. But no body also!
  • #18: /* just like java, static, can’t be instantiated ! But the sealed class subclasses can! */
  • #19: TRS, Deep copy semantics
  • #20: Many ways to create a singleton… double checked locked thread safe
  • #24: Separate class files are created and they then delegate to PackageName.class
  • #26: Cast itself may work but calling methods on the casted class may CCE
  • #27: Infix only one parameter
  • #32: Difference with java inner class visibility, tried to extend a protected inner class in Kotlin couldn’t do it
  • #33: Interfaces are obviously always open for implementation!
  • #36: Same for BSCIFLD, and int to Int to int, except nullable types!
  • #38: Abstraction on a java field
  • #40: Tony Hoares…
  • #44: intrinsic functions on the jvm
  • #45: Casting and cursing…
  • #47: Unit returns singleton “Unit”. Nothing returns nothing
  • #48: String.valueOf(x).matches("^[1-9]$|^[1-9][0-9]$|^(100)$") – pattern or Java Short circuit!
  • #50: Mr object pulled from java
  • #51: mjt = platform type, try to dereference, in java OK in a sout!
  • #52: mjt now uses the elvis operator to check for nullability
  • #53: /* just like java, static, can’t be instantiated ! But the sealed class subclasses can! */
  • #54: Static = static. Instances = instance of a singleton (everything in kotlin is an object)
  • #55: No multicatch in Kotlin!
  • #58: Compare to jdk / covariant/invariant/PECS/CIPO, thread-safe?
  • #59: variance annotation
  • #60: variance annotation
  • #61: Accept E or subtypes (Covariance) in Java
  • #65: Function reference, pass as value to other fun
  • #66: Function reference, pass as value to other fun
  • #67: Function reference, pass as value to other fun
  翻译: