SlideShare a Scribd company logo
Kotlin. Decompiled.
Who am I?
• Klymenko Ruslan
• Software engineer at ‘zorallabs’
• Passionate about JVM
• Ternopil JUG co-founder
• Founder of ‘Dobroe IT’
Who knows about kotlin?
Who have worked with
kotlin?
Who use kotlin in everyday
practice?
So… What is Kotlin?
• Modern
• Jvm-based (not only! =) )
• Statically-typed
And… Why Kotlin?
Interoperability. Kotlin is same as java. It also
produces bytecode.
But… Wait a minute. What is bytecode?
What are mnemonics?
What is decompilation?
• The reverse process of compilation.
• Takes executable code as input.
• Produces high-level code as output.
What is this talk about?
• We’re going to decompile Kotlin code to Java analogue.
• It will give a better understanding of what’s going on under the hood.
What is this talk about?
• It will give a feeling when Kotlin is an appropriate solution which
minimizes boilerplate.
• It will show Kotlin’s weak points(if there any)
So… Let’s start from the beginning!
fun main(args: Array<String>) {
println("Hey, folks!")
} ????
Hello, world!
@Metadata(
mv = {1, 1, 9},
bv = {1, 0, 2},
k = 2,
d1 = {"u0000u0014nu0000n...."},
d2 = {"main", "", "args", "", "", …}
)
public final class Test1Kt {
public static final void main(@NotNull String[] args) {
Intrinsics.checkParameterIsNotNull(args, "args");
String var1 = "Hey, folks!";
System.out.println(var1);
}
}
What’s wrong here?
object Test {
fun main(args: Array<String>) {
}
}
What’s wrong here?
public final class Test {
public static final Test INSTANCE;
public final void main(@NotNull String[] args) {
Intrinsics.checkParameterIsNotNull(args, "args");
}
static {
Test var0 = new Test();
INSTANCE = var0;
}
}
@JvmStatic
object Test {
@JvmStatic
fun main(args: Array<String>) {
}
}
Extension methods or how to add functionality
even to final types in a natural way.
fun String.isTest() =
this == "test"
????
Extension methods or how to add functionality
even to final types in a natural way.
public final class Test2Kt {
public static final boolean isTest(@NotNull String
$receiver) {
Intrinsics.checkParameterIsNotNull($receiver,
"$receiver");
return Intrinsics.areEqual($receiver, "test");
}
}
Extension methods from client’s point of
view
fun testExtension() {
"".isTest()
}
Extension methods from client’s point of
view
public static final void testExtension() {
isTest("");
}
The simplest class ever. But wait.
What’s wrong here?
class Test3(test: String)
????
The simplest class ever.
public final class Test3 {
public Test3(@NotNull String test) {
Intrinsics.checkParameterIsNotNull(test, "test");
}
}
The simplest bean ever. The correct one
class Test4(val test: String)
The simplest bean ever. The correct one
public final class Test4 {
@NotNull
private final String test;
@NotNull
public final String getTest() {
return this.test;
}
public Test4(@NotNull String test) {
Intrinsics.checkParameterIsNotNull(test, "test");
this.test = test;
}
}
The simplest bean ever. The correct one
class Test4(var test: String) ????
The simplest bean ever + DEFAULT arg
class Test4(val test: String = "")
The simplest bean ever + DEFAULT arg
public final class Test4 {
…
// $FF: synthetic method
public Test4(String var1, int var2,DefaultConstructorMarker var3) {
if ((var2 & 1) != 0) {
var1 = "";
}
this(var1);
}
public Test4(@NotNull String test) {
...
this.test = test;
}
public Test4() {
this((String)null, 1, (DefaultConstructorMarker)null);
}
}
Default constructor args. Call from java code.
class Test4(val test: String = "")
void test() {
new Test4("test");
new Test4();
}
Default method args. Call from java code.
class Test5 {
fun x(x: Int = 5) { }
}
Default method args. Call from java code.
class Test5 {
fun x(x: Int = 5) { }
}
void test() {
new Test5().x();
}
Default method args. Call from java code.
class Test5 {
fun x(x: Int = 5) { }
}
void test() {
new Test5().x();
}
Default method args. Call from java code.
public final class Test5 {
public final void x(int x) {
}
public static void x$default(Test5 var0,
int var1,
int var2,
Object var3) {
if ((var2 & 1) != 0) {
var1 = 5;
}
var0.x(var1);
}
}
Default method args. Correct call from java
code.
void test() {
new Test5().x();
}
class Test5 {
@JvmOverloads
fun x(x: Int = 5) { }
}
Give me more DATA!!!
data class Test6(val x: Int = 0, val y: Int = 0)
Give me more DATA!!!
public final class Test6 {
private final int x; private final int y;
// + getX(), getY(), constructor with both parameters
// + toString(), hashCode(), equals(Object obj)
public final int component1() {
return this.x;
}
public final int component2() {
return this.y;
}
@NotNull
public final Test6 copy(int x, int y) {
return new Test6(x, y);
}
}
Destructurization
fun test() {
val (a,b,c) = Test6()
}
public final void test() {
Test6 var4 = new Test6(0, 0, (Test4)null, 7,
DefaultConstructorMarker)null);
int var1 = var4.component1();
int var2 = var4.component2();
Test4 c = var4.component3();
}
Let my Kotlin go
fun test7() = Any().let {
it.toString()
this.toString()
}
Let my Kotlin go
public final String test7() {
Object var1 = new Object();
var1.toString();
return this.toString();
}
Let’s think about it
????fun test() = null.let { println(it) }
Let’s think about it
public static final void test() {
Object var0 = null;
Void it = (Void)var0;
System.out.println(it);
}
The power of primitives
fun test() = 1
public final int test () {
return 1;
}
The power of primitives
fun test(): Int {
val x: Int = 1
return x
}
public final int test() {
int x = 1;
return x;
}
The power of primitives
fun test(): Int {
val x: Int = 1
x.toString()
x.compareTo(2)
x.hashCode()
return x
}
????
The power of primitives
fun test(): Int {
val x: Int = 1
x.toString()
x.compareTo(2)
x.hashCode()
return x
}
public final int test() {
int x = 1;
String.valueOf(x);
Intrinsics.compare(x, 2);
Integer.valueOf(x).hashCode();
return x;
}
Kotlin decompiled
Nullable types
fun test(value: Int?) {
val test1 = value ?: 0
}
????
Nullable types
public static final void test(@Nullable Integer value)
{
int test1 = value != null ? value.intValue() : 0;
}
Nullable types
fun test(value: Int?) {
val test1 = value!!
}
????
Nullable types
public static final void test(@Nullable Integer
value) {
if (value == null) {
Intrinsics.throwNpe();
}
int test1 = value.intValue();
}
Cast me babe
val x = Any() is String
val y: Any = 1
val z: Int = y as Int
Cast me babe
private static final boolean x =
new Object() instanceof String;
@NotNull
private static final Object y = Integer.valueOf(1);
private static final int z = 1;
Cast me babe. And please, be smart!
fun test() {
val any = "test" as Any
if(any is String) {
any.trim()
any.substringBefore("test")
}
}
Cast me babe. And please, be smart!
public static final void test() {
Object any = (Object)"test";
String var1 = (String)any;
StringsKt.trim((CharSequence)var1).toString();
StringsKt.substringBefore$default((String)any, "test",
(String)null, 2,
(Object)null);
}
When
fun test(x: Any) {
when(x) {
0 -> println(0)
is Int -> println("Int")
is Number -> println("Number")
else -> println("Not an int")
}
}
????
When
public static final void test(@NotNull Object x) {
Intrinsics.checkParameterIsNotNull(x, "x");
if (Intrinsics.areEqual(x, Integer.valueOf(0))) {
byte var2 = 0;
System.out.println(var2);
} else {
String var3;
if (x instanceof Integer) {
var3 = "Int";
System.out.println(var3);
} else if (x instanceof Number) {
var3 = "Number";
System.out.println(var3);
} else {
var3 = "Not an int";
System.out.println(var3);
}
}
}
Where is my ternary?
fun test (value: Int) = value == 1 ? 1 : 0
public static final int test (int value) {
return value == 1 ? 1 : 0;
}
Where is my ternary?
fun test (value: Int) = if(value == 1) 1 else 0
Range
public final class Test14Kt {
@NotNull
private static final IntRange range;
@NotNull
public static final IntRange
getRange() {
return range;
}
static {
byte var0 = 1;
range = new IntRange(var0, 100);
}
}
val range = 1 .. 100
Range
????val result = 50 in 1..100
Range
public final class Test14Kt {
private static final boolean result = true;
public static final boolean getResult() {
return result;
}
}
Range
fun test14(){
for(x in 1 .. 100) { }
}
Range
public static final void test14() {
int var0 = 1;
for(byte var1 = 101; var0 < var1; ++var0) {
;
}
}
Range
fun test14(){
for(x in 1 .. 100 step 1) { }
}
Range
public static final void test14() {
byte var3 = 1;
IntProgression var10000 =
RangesKt.step(
(IntProgression)(new IntRange(var3, 100)), 1);
int x = var10000.getFirst();
int var1 = var10000.getLast();
int var2 = var10000.getStep();
if (var2 > 0) {
if (x > var1) {
return;
}
} else if (x < var1) {
return;
}
while(x != var1) {
x += var2;
}
}
Inlines
fun test15() {
println(test15_1 { it * 2 })
}
fun test15_1(num: (Int) -> Int) = num(2)
Inlines
public static final void test15() {
int var0 = test15_1((Function1)null.INSTANCE);
System.out.println(var0);
}
public static final int test15_1(@NotNull Function1 num)
{
Intrinsics.checkParameterIsNotNull(num, "num");
return ((Number)num.invoke(2)).intValue();
}
Inlines
fun test15() {
println(test15_1 { it * 2 })
}
inline fun test15_1(num: (Int) -> Int) = num(2)
Inlines
public static final void test15() {
int it = 2;
int var3 = it * 2;
System.out.println(var3);
}
public static final int test15_1(@NotNull Function1 num)
{
Intrinsics.checkParameterIsNotNull(num, "num");
return ((Number)num.invoke(2)).intValue();
}
Inlines. Noinline
fun test15() {
println(test15_1({ it + 1 }, { it * 2 }))
}
inline fun test15_1(num: (Int) -> Int, noinline num2:
(Int) -> Int)
= num(1) + num2(1)
Inlines. Noinline
public static final void test15() {
Function1 num2$iv = (Function1)null.INSTANCE;
int it = 1;
int var4 = it + 1 +
((Number)num2$iv.invoke(1)).intValue();
System.out.println(var4);
}
public static final int test15_1(@NotNull Function1 num,
@NotNull Function1 num2) {
Intrinsics.checkParameterIsNotNull(num, "num");
Intrinsics.checkParameterIsNotNull(num2, "num2");
return ((Number)num.invoke(1)).intValue() +
((Number)num2.invoke(1)).intValue();
}
Overloading with generics
public void sort(List<String> x) {}
public void sort(List<Integer> x) {}
Overloading with generics
fun sort(x: List<Any>) {}
fun sort(x: List<Int>) {}
Overloading with generics
fun sort(x: List<Any>) {}
@JvmName("sortInt")
fun sort(x: List<Int>) {}
Overloading of operators
class IntArray(val data: Array<Int>) {
operator fun plus(value: Int) = data + value
fun test() {
val intArr = IntArray(arrayOf(1))
val newArr = intArr + 2
}
}
Overloading of operators
public final class IntArray {
@NotNull
private final Integer[] data;
@NotNull
public final Integer[] plus(int value) {
return (Integer[])ArraysKt.plus((Object[])this.data, value);
}
public final void test() {
IntArray intArr = new IntArray(new Integer[]{Integer.valueOf(1)});
Integer[] newArr = intArr.plus(2);
}
@NotNull
public final Integer[] getData() {
return this.data;
}
public IntArray(@NotNull Integer[] data) {
Intrinsics.checkParameterIsNotNull(data, "data");
super();
this.data = data;
}
}
Delegation using ‘by’
interface Interf {
fun xx(): Int
}
class A: Interf {
override fun xx() = 1
}
class B(a: A): Interf by a
????
Delegation using ‘by’
public interface Interf { int xx();}
public final class A implements Interf { public int xx() { return 1; }}
public final class B implements Interf {
private final A $$delegate_0;
public B(@NotNull A a) {
Intrinsics.checkParameterIsNotNull(a, "a");
super();
this.$$delegate_0 = a;
}
public int xx() {
return this.$$delegate_0.xx();
}
Recursion
fun factorial(value: Int): Int =
if(value <= 1) 1
else value * factorial(value -1)
Recursion
public static final int factorial(int value) {
return value <= 1 ?
1 :
value * factorial(value - 1);
}
Tail recursion
????tailrec fun factorial(value: Int)
: Int =
if(value <= 1) 1
else value * factorial(value -1)
Tail recursion
public static final int factorial(int value) {
return value <= 1 ?
1 :
value * factorial(value - 1);
}
Tail recursion
tailrec fun factorial(value: Int, acc: Int): Int =
if(value <= 1) acc * value
else factorial(value - 1, acc * value)
Tail recursion
public static final int factorial(int value, int acc) {
while(value > 1) {
int var10000 = value - 1;
acc *= value;
value = var10000;
}
return acc * value;
}
Cons of Kotlin
• Learning curve
• Slow compilation speed
• Smaller community with
comparison to java
Pros of Kotlin
• Reduces boilerplate
• Compatible with jvm
• Competitive performance
• Elegant
• Best IDE integration
• Choice №1 for android
Finish
Kotlin decompiled

More Related Content

PDF
Comparing JVM languages
PDF
Idiomatic Kotlin
PDF
Kotlin, why?
PDF
Feel of Kotlin (Berlin JUG 16 Apr 2015)
PDF
Easy Going Groovy 2nd season on DevLOVE
PPTX
Unit testing concurrent code
PPS
Class method
PDF
Sam wd programs
Comparing JVM languages
Idiomatic Kotlin
Kotlin, why?
Feel of Kotlin (Berlin JUG 16 Apr 2015)
Easy Going Groovy 2nd season on DevLOVE
Unit testing concurrent code
Class method
Sam wd programs

What's hot (20)

PPTX
Java fundamentals
PDF
Let's go Developer 2011 sendai Let's go Java Developer (Programming Language ...
PPT
Chapter 2 Java Methods
KEY
Groovy 1.8の新機能について
PDF
Java programs
PPT
Exception handling
PDF
Java Fundamentals
PDF
Privet Kotlin (Windy City DevFest)
PDF
Important java programs(collection+file)
PPTX
Java generics
PDF
Lezione03
PDF
The Ring programming language version 1.7 book - Part 36 of 196
PPT
Learning Java 1 – Introduction
PPTX
16. Java stacks and queues
PDF
The Ring programming language version 1.4.1 book - Part 9 of 31
PPTX
Java generics final
KEY
Clojure Intro
DOC
Final JAVA Practical of BCA SEM-5.
PPTX
Kotlin as a Better Java
Java fundamentals
Let's go Developer 2011 sendai Let's go Java Developer (Programming Language ...
Chapter 2 Java Methods
Groovy 1.8の新機能について
Java programs
Exception handling
Java Fundamentals
Privet Kotlin (Windy City DevFest)
Important java programs(collection+file)
Java generics
Lezione03
The Ring programming language version 1.7 book - Part 36 of 196
Learning Java 1 – Introduction
16. Java stacks and queues
The Ring programming language version 1.4.1 book - Part 9 of 31
Java generics final
Clojure Intro
Final JAVA Practical of BCA SEM-5.
Kotlin as a Better Java
Ad

Similar to Kotlin decompiled (20)

PDF
Kotlin Bytecode Generation and Runtime Performance
PDF
Kotlin for Android Developers - 3
PPTX
KotlinForJavaDevelopers-UJUG.pptx
PDF
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
PDF
Kotlin Developer Starter in Android projects
PDF
Bulletproofing your foot for Kotlin
PPTX
Lies Told By The Kotlin Compiler
PPTX
Benefits of Kotlin
PDF
Intro to Kotlin
PDF
2 kotlin vs. java: what java has that kotlin does not
PDF
Kotlin Basics - Apalon Kotlin Sprint Part 2
PDF
Kotlin intro
PDF
Kotlin: forse è la volta buona (Trento)
PDF
Effective Java and Kotlin
PPTX
Lies Told By The Kotlin Compiler
PDF
Develop your next app with kotlin @ AndroidMakersFr 2017
PPTX
Kotlin on android
PPTX
Kotlin / Android Update
PPTX
Introduction to kotlin + spring boot demo
PDF
Having Fun with Kotlin Android - DILo Surabaya
Kotlin Bytecode Generation and Runtime Performance
Kotlin for Android Developers - 3
KotlinForJavaDevelopers-UJUG.pptx
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android projects
Bulletproofing your foot for Kotlin
Lies Told By The Kotlin Compiler
Benefits of Kotlin
Intro to Kotlin
2 kotlin vs. java: what java has that kotlin does not
Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin intro
Kotlin: forse è la volta buona (Trento)
Effective Java and Kotlin
Lies Told By The Kotlin Compiler
Develop your next app with kotlin @ AndroidMakersFr 2017
Kotlin on android
Kotlin / Android Update
Introduction to kotlin + spring boot demo
Having Fun with Kotlin Android - DILo Surabaya
Ad

Recently uploaded (20)

PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Sustainable Sites - Green Building Construction
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
additive manufacturing of ss316l using mig welding
PDF
Well-logging-methods_new................
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Lecture Notes Electrical Wiring System Components
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
UNIT 4 Total Quality Management .pptx
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
bas. eng. economics group 4 presentation 1.pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
OOP with Java - Java Introduction (Basics)
Sustainable Sites - Green Building Construction
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Model Code of Practice - Construction Work - 21102022 .pdf
additive manufacturing of ss316l using mig welding
Well-logging-methods_new................
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Mechanical Engineering MATERIALS Selection
Lecture Notes Electrical Wiring System Components
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
UNIT 4 Total Quality Management .pptx
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Embodied AI: Ushering in the Next Era of Intelligent Systems

Kotlin decompiled

  • 2. Who am I? • Klymenko Ruslan • Software engineer at ‘zorallabs’ • Passionate about JVM • Ternopil JUG co-founder • Founder of ‘Dobroe IT’
  • 3. Who knows about kotlin?
  • 4. Who have worked with kotlin?
  • 5. Who use kotlin in everyday practice?
  • 6. So… What is Kotlin? • Modern • Jvm-based (not only! =) ) • Statically-typed
  • 8. Interoperability. Kotlin is same as java. It also produces bytecode.
  • 9. But… Wait a minute. What is bytecode?
  • 11. What is decompilation? • The reverse process of compilation. • Takes executable code as input. • Produces high-level code as output.
  • 12. What is this talk about? • We’re going to decompile Kotlin code to Java analogue. • It will give a better understanding of what’s going on under the hood.
  • 13. What is this talk about? • It will give a feeling when Kotlin is an appropriate solution which minimizes boilerplate. • It will show Kotlin’s weak points(if there any)
  • 14. So… Let’s start from the beginning! fun main(args: Array<String>) { println("Hey, folks!") } ????
  • 15. Hello, world! @Metadata( mv = {1, 1, 9}, bv = {1, 0, 2}, k = 2, d1 = {"u0000u0014nu0000n...."}, d2 = {"main", "", "args", "", "", …} ) public final class Test1Kt { public static final void main(@NotNull String[] args) { Intrinsics.checkParameterIsNotNull(args, "args"); String var1 = "Hey, folks!"; System.out.println(var1); } }
  • 16. What’s wrong here? object Test { fun main(args: Array<String>) { } }
  • 17. What’s wrong here? public final class Test { public static final Test INSTANCE; public final void main(@NotNull String[] args) { Intrinsics.checkParameterIsNotNull(args, "args"); } static { Test var0 = new Test(); INSTANCE = var0; } }
  • 18. @JvmStatic object Test { @JvmStatic fun main(args: Array<String>) { } }
  • 19. Extension methods or how to add functionality even to final types in a natural way. fun String.isTest() = this == "test" ????
  • 20. Extension methods or how to add functionality even to final types in a natural way. public final class Test2Kt { public static final boolean isTest(@NotNull String $receiver) { Intrinsics.checkParameterIsNotNull($receiver, "$receiver"); return Intrinsics.areEqual($receiver, "test"); } }
  • 21. Extension methods from client’s point of view fun testExtension() { "".isTest() }
  • 22. Extension methods from client’s point of view public static final void testExtension() { isTest(""); }
  • 23. The simplest class ever. But wait. What’s wrong here? class Test3(test: String) ????
  • 24. The simplest class ever. public final class Test3 { public Test3(@NotNull String test) { Intrinsics.checkParameterIsNotNull(test, "test"); } }
  • 25. The simplest bean ever. The correct one class Test4(val test: String)
  • 26. The simplest bean ever. The correct one public final class Test4 { @NotNull private final String test; @NotNull public final String getTest() { return this.test; } public Test4(@NotNull String test) { Intrinsics.checkParameterIsNotNull(test, "test"); this.test = test; } }
  • 27. The simplest bean ever. The correct one class Test4(var test: String) ????
  • 28. The simplest bean ever + DEFAULT arg class Test4(val test: String = "")
  • 29. The simplest bean ever + DEFAULT arg public final class Test4 { … // $FF: synthetic method public Test4(String var1, int var2,DefaultConstructorMarker var3) { if ((var2 & 1) != 0) { var1 = ""; } this(var1); } public Test4(@NotNull String test) { ... this.test = test; } public Test4() { this((String)null, 1, (DefaultConstructorMarker)null); } }
  • 30. Default constructor args. Call from java code. class Test4(val test: String = "") void test() { new Test4("test"); new Test4(); }
  • 31. Default method args. Call from java code. class Test5 { fun x(x: Int = 5) { } }
  • 32. Default method args. Call from java code. class Test5 { fun x(x: Int = 5) { } } void test() { new Test5().x(); }
  • 33. Default method args. Call from java code. class Test5 { fun x(x: Int = 5) { } } void test() { new Test5().x(); }
  • 34. Default method args. Call from java code. public final class Test5 { public final void x(int x) { } public static void x$default(Test5 var0, int var1, int var2, Object var3) { if ((var2 & 1) != 0) { var1 = 5; } var0.x(var1); } }
  • 35. Default method args. Correct call from java code. void test() { new Test5().x(); } class Test5 { @JvmOverloads fun x(x: Int = 5) { } }
  • 36. Give me more DATA!!! data class Test6(val x: Int = 0, val y: Int = 0)
  • 37. Give me more DATA!!! public final class Test6 { private final int x; private final int y; // + getX(), getY(), constructor with both parameters // + toString(), hashCode(), equals(Object obj) public final int component1() { return this.x; } public final int component2() { return this.y; } @NotNull public final Test6 copy(int x, int y) { return new Test6(x, y); } }
  • 38. Destructurization fun test() { val (a,b,c) = Test6() } public final void test() { Test6 var4 = new Test6(0, 0, (Test4)null, 7, DefaultConstructorMarker)null); int var1 = var4.component1(); int var2 = var4.component2(); Test4 c = var4.component3(); }
  • 39. Let my Kotlin go fun test7() = Any().let { it.toString() this.toString() }
  • 40. Let my Kotlin go public final String test7() { Object var1 = new Object(); var1.toString(); return this.toString(); }
  • 41. Let’s think about it ????fun test() = null.let { println(it) }
  • 42. Let’s think about it public static final void test() { Object var0 = null; Void it = (Void)var0; System.out.println(it); }
  • 43. The power of primitives fun test() = 1 public final int test () { return 1; }
  • 44. The power of primitives fun test(): Int { val x: Int = 1 return x } public final int test() { int x = 1; return x; }
  • 45. The power of primitives fun test(): Int { val x: Int = 1 x.toString() x.compareTo(2) x.hashCode() return x } ????
  • 46. The power of primitives fun test(): Int { val x: Int = 1 x.toString() x.compareTo(2) x.hashCode() return x } public final int test() { int x = 1; String.valueOf(x); Intrinsics.compare(x, 2); Integer.valueOf(x).hashCode(); return x; }
  • 48. Nullable types fun test(value: Int?) { val test1 = value ?: 0 } ????
  • 49. Nullable types public static final void test(@Nullable Integer value) { int test1 = value != null ? value.intValue() : 0; }
  • 50. Nullable types fun test(value: Int?) { val test1 = value!! } ????
  • 51. Nullable types public static final void test(@Nullable Integer value) { if (value == null) { Intrinsics.throwNpe(); } int test1 = value.intValue(); }
  • 52. Cast me babe val x = Any() is String val y: Any = 1 val z: Int = y as Int
  • 53. Cast me babe private static final boolean x = new Object() instanceof String; @NotNull private static final Object y = Integer.valueOf(1); private static final int z = 1;
  • 54. Cast me babe. And please, be smart! fun test() { val any = "test" as Any if(any is String) { any.trim() any.substringBefore("test") } }
  • 55. Cast me babe. And please, be smart! public static final void test() { Object any = (Object)"test"; String var1 = (String)any; StringsKt.trim((CharSequence)var1).toString(); StringsKt.substringBefore$default((String)any, "test", (String)null, 2, (Object)null); }
  • 56. When fun test(x: Any) { when(x) { 0 -> println(0) is Int -> println("Int") is Number -> println("Number") else -> println("Not an int") } } ????
  • 57. When public static final void test(@NotNull Object x) { Intrinsics.checkParameterIsNotNull(x, "x"); if (Intrinsics.areEqual(x, Integer.valueOf(0))) { byte var2 = 0; System.out.println(var2); } else { String var3; if (x instanceof Integer) { var3 = "Int"; System.out.println(var3); } else if (x instanceof Number) { var3 = "Number"; System.out.println(var3); } else { var3 = "Not an int"; System.out.println(var3); } } }
  • 58. Where is my ternary? fun test (value: Int) = value == 1 ? 1 : 0 public static final int test (int value) { return value == 1 ? 1 : 0; }
  • 59. Where is my ternary? fun test (value: Int) = if(value == 1) 1 else 0
  • 60. Range public final class Test14Kt { @NotNull private static final IntRange range; @NotNull public static final IntRange getRange() { return range; } static { byte var0 = 1; range = new IntRange(var0, 100); } } val range = 1 .. 100
  • 61. Range ????val result = 50 in 1..100
  • 62. Range public final class Test14Kt { private static final boolean result = true; public static final boolean getResult() { return result; } }
  • 63. Range fun test14(){ for(x in 1 .. 100) { } }
  • 64. Range public static final void test14() { int var0 = 1; for(byte var1 = 101; var0 < var1; ++var0) { ; } }
  • 65. Range fun test14(){ for(x in 1 .. 100 step 1) { } }
  • 66. Range public static final void test14() { byte var3 = 1; IntProgression var10000 = RangesKt.step( (IntProgression)(new IntRange(var3, 100)), 1); int x = var10000.getFirst(); int var1 = var10000.getLast(); int var2 = var10000.getStep(); if (var2 > 0) { if (x > var1) { return; } } else if (x < var1) { return; } while(x != var1) { x += var2; } }
  • 67. Inlines fun test15() { println(test15_1 { it * 2 }) } fun test15_1(num: (Int) -> Int) = num(2)
  • 68. Inlines public static final void test15() { int var0 = test15_1((Function1)null.INSTANCE); System.out.println(var0); } public static final int test15_1(@NotNull Function1 num) { Intrinsics.checkParameterIsNotNull(num, "num"); return ((Number)num.invoke(2)).intValue(); }
  • 69. Inlines fun test15() { println(test15_1 { it * 2 }) } inline fun test15_1(num: (Int) -> Int) = num(2)
  • 70. Inlines public static final void test15() { int it = 2; int var3 = it * 2; System.out.println(var3); } public static final int test15_1(@NotNull Function1 num) { Intrinsics.checkParameterIsNotNull(num, "num"); return ((Number)num.invoke(2)).intValue(); }
  • 71. Inlines. Noinline fun test15() { println(test15_1({ it + 1 }, { it * 2 })) } inline fun test15_1(num: (Int) -> Int, noinline num2: (Int) -> Int) = num(1) + num2(1)
  • 72. Inlines. Noinline public static final void test15() { Function1 num2$iv = (Function1)null.INSTANCE; int it = 1; int var4 = it + 1 + ((Number)num2$iv.invoke(1)).intValue(); System.out.println(var4); } public static final int test15_1(@NotNull Function1 num, @NotNull Function1 num2) { Intrinsics.checkParameterIsNotNull(num, "num"); Intrinsics.checkParameterIsNotNull(num2, "num2"); return ((Number)num.invoke(1)).intValue() + ((Number)num2.invoke(1)).intValue(); }
  • 73. Overloading with generics public void sort(List<String> x) {} public void sort(List<Integer> x) {}
  • 74. Overloading with generics fun sort(x: List<Any>) {} fun sort(x: List<Int>) {}
  • 75. Overloading with generics fun sort(x: List<Any>) {} @JvmName("sortInt") fun sort(x: List<Int>) {}
  • 76. Overloading of operators class IntArray(val data: Array<Int>) { operator fun plus(value: Int) = data + value fun test() { val intArr = IntArray(arrayOf(1)) val newArr = intArr + 2 } }
  • 77. Overloading of operators public final class IntArray { @NotNull private final Integer[] data; @NotNull public final Integer[] plus(int value) { return (Integer[])ArraysKt.plus((Object[])this.data, value); } public final void test() { IntArray intArr = new IntArray(new Integer[]{Integer.valueOf(1)}); Integer[] newArr = intArr.plus(2); } @NotNull public final Integer[] getData() { return this.data; } public IntArray(@NotNull Integer[] data) { Intrinsics.checkParameterIsNotNull(data, "data"); super(); this.data = data; } }
  • 78. Delegation using ‘by’ interface Interf { fun xx(): Int } class A: Interf { override fun xx() = 1 } class B(a: A): Interf by a ????
  • 79. Delegation using ‘by’ public interface Interf { int xx();} public final class A implements Interf { public int xx() { return 1; }} public final class B implements Interf { private final A $$delegate_0; public B(@NotNull A a) { Intrinsics.checkParameterIsNotNull(a, "a"); super(); this.$$delegate_0 = a; } public int xx() { return this.$$delegate_0.xx(); }
  • 80. Recursion fun factorial(value: Int): Int = if(value <= 1) 1 else value * factorial(value -1)
  • 81. Recursion public static final int factorial(int value) { return value <= 1 ? 1 : value * factorial(value - 1); }
  • 82. Tail recursion ????tailrec fun factorial(value: Int) : Int = if(value <= 1) 1 else value * factorial(value -1)
  • 83. Tail recursion public static final int factorial(int value) { return value <= 1 ? 1 : value * factorial(value - 1); }
  • 84. Tail recursion tailrec fun factorial(value: Int, acc: Int): Int = if(value <= 1) acc * value else factorial(value - 1, acc * value)
  • 85. Tail recursion public static final int factorial(int value, int acc) { while(value > 1) { int var10000 = value - 1; acc *= value; value = var10000; } return acc * value; }
  • 86. Cons of Kotlin • Learning curve • Slow compilation speed • Smaller community with comparison to java
  • 87. Pros of Kotlin • Reduces boilerplate • Compatible with jvm • Competitive performance • Elegant • Best IDE integration • Choice №1 for android