It's time for Kotlin

Agenda
- What is Kotlin?
- #1 Type inference
- #2 Data structures
- #3 Default parameters
- #4 Null safety
- #5 Extenion functions
- #6 Lazy intialization
- #7 Operator overloading
What is kotlin?
- Statically typed programming language
for the JVM
What is kotlin?
- Statically typed programming language
for the JVM - Developed by JetBrains
What is kotlin?
- Statically typed programming language
for the JVM - Developed by JetBrains
- 100% interoperability with Java
What is kotlin?
- Statically typed programming language
for the JVM - Developed by JetBrains
- 100% interoperability with Java
- It targets JVM 6
What is kotlin?
- Statically typed programming language
for the JVM - Developed by JetBrains
- 100% interoperability with Java
- It targets JVM 6
- Supports functional programming with zero-overhead lambdas
#1 Type inference
#1 Type inference
String firstName = "Adam";
char middle = 'c';
String lastName = "Brown";
int age = 15;
Map<String,List<String>> fruitsColors = new HashMap<>();
List<String> colors = new ArrayList<>();
colors.add("red");
colors.add("green");
fruitsColors.put("apple", colors);
J
A
V
A
#1 Type inference
val firstName = "Adam" // String
val middle = 'c' // Character
val lastName = "Brown" // String
val age = 15 // Integer
val fruitsColors = mapOf("apple" to listOf("red", "green"))
String firstName = "Adam";
char middle = 'c';
String lastName = "Brown";
int age = 15;
Map<String,List<String>> fruitsColors = new HashMap<>();
List<String> colors = new ArrayList<>();
colors.add("red");
colors.add("green");
fruitsColors.put("apple", colors);
J
A
V
A
K
T
#2 Data structures
#2 Data structures
public class JavaUser {
public final String firstName;
public final String lastName;
public final Integer age;
public JavaUser(String firstName, String lastName, Integer age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
JavaUser javaUser = (JavaUser) o;
if (!firstName.equals(javaUser.firstName)) return false;
if (!lastName.equals(javaUser.lastName)) return false;
return age.equals(javaUser.age);
}
@Override
public int hashCode() {
int result = firstName.hashCode();
result = 31 * result + lastName.hashCode();
result = 31 * result + age.hashCode();
return result;
}
@Override
public String toString() {
return "JavaUser{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", age=" + age +
'}';
}
}
#2 Data structures
public class JavaUser {
public final String firstName;
public final String lastName;
public final Integer age;
public JavaUser(String firstName, String lastName, Integer age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
JavaUser javaUser = (JavaUser) o;
if (!firstName.equals(javaUser.firstName)) return false;
if (!lastName.equals(javaUser.lastName)) return false;
return age.equals(javaUser.age);
}
@Override
public int hashCode() {
int result = firstName.hashCode();
result = 31 * result + lastName.hashCode();
result = 31 * result + age.hashCode();
return result;
}
@Override
public String toString() {
return "JavaUser{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", age=" + age +
'}';
}
}
data class KotlinUser(val firstName: String,
val lastName: String,
val age: Int)J
A
V
A
K
T
#3 Default parameters
JavaUser createUserWithFirstName(String firstName) {
return new JavaUser(firstName, "Brown", 15);
}
JavaUser createUserWithLastName(String lastName) {
return new JavaUser("Jacob", lastName, 15);
}
createUserWithFirstName("Alan")
createUserWithLastName("Jones")J
A
V
A
#3 Default parameters
JavaUser createUserWithFirstName(String firstName) {
return new JavaUser(firstName, "Brown", 15);
}
JavaUser createUserWithLastName(String lastName) {
return new JavaUser("Jacob", lastName, 15);
}
createUserWithFirstName("Alan")
createUserWithLastName("Jones")fun createUser(firstName: String = "Adam",
lastName: String = "Brown",
age: Int = 15): KotlinUser {
return KotlinUser(firstName, lastName, age)
}
createUser(firstName = "Jacob")
createUser(lastName = "Jones")
createUser(age = 25, firstName = "Alan")
J
A
V
A
K
T
#4 Null safety
interface Person {
Person getFather();
String getName();
}
class FatherNameDisplayer {
public String display(Person person) {
Person father = person.getFather();
if (father != null) {
String name = father.getName();
return name;
}
return "Name not known";
}
}JAVA
#4 Nullsafety
interface Person {
Person getFather();
String getName();
}
class FatherNameDisplayer {
public String display(Person person) {
Person father = person.getFather();
if (father != null) {
String name = father.getName();
return name;
}
return "Name not known";
}
}interface Person {
fun getFather(): Person?
fun getName(): String?
}JAVA
KT
#4 Nullsafety
interface Person {
Person getFather();
String getName();
}
class FatherNameDisplayer {
public String display(Person person) {
Person father = person.getFather();
if (father != null) {
String name = father.getName();
return name;
}
return "Name not known";
}
}interface Person {
fun getFather(): Person?
fun getName(): String?
}
class FatherNameDisplayer {
fun display(person: Person): String {
val father = person.getFather()
if (father != null) {
val name = father.getName()
return name //HERE!!!
}
return "Name not known"
}
}JAVA
KT
#4 Nullsafety
interface Person {
Person getFather();
String getName();
}
class FatherNameDisplayer {
public String display(Person person) {
Person father = person.getFather();
if (father != null) {
String name = father.getName();
return name;
}
return "Name not known";
}
}interface Person {
fun getFather(): Person?
fun getName(): String?
}
class FatherNameDisplayer {
fun display(person: Person)
= person.getFather()?.getName()
?: "Name not known"
}JAVA
KT
#5 Extension functions
Integer max = Collections.max(list);
Integer result = Collections.binarySearch(otherList, max);JAVA
#5 Extension functions
Integer max = Collections.max(list);
Integer result = Collections.binarySearch(otherList, max);val max = list.max();
val result = otherList.binarySearch(max);JAVA
KT
#5 Extension functions
Integer max = Collections.max(list);
Integer result = Collections.binarySearch(otherList, max);val max = list.max();
val result = otherList.binarySearch(max);JAVA
KT
fun List<Int>.max(): Int? {
return Collections.max(this)
}#6 Lazy initialization
public class MainActivity extends AppCompatActivity {
private MainController controller;
@Override
void onCreate() {
super.onCreate();
controller = new MainController(getIntent());
controller.onCreate()
}
@Override
void onDestroy() {
controller.onDestroy();
super.onDestroy();
}
}JAVA
#6 Lazy initialization
public class MainActivity extends AppCompatActivity {
private MainController controller;
@Override
void onCreate() {
super.onCreate();
controller = new MainController(getIntent());
controller.onCreate()
}
@Override
void onDestroy() {
controller.onDestroy();
super.onDestroy();
}
}class KotlinActivity : AppCompatActivity() {
private val controller by lazy {
MainController(getIntent())
}
override fun onCreate() {
super.onCreate()
controller.onCreate()
}
override fun onDestroy() {
controller.onDestroy()
super.onDestroy()
}
}JAVA
KT
#7 operators overloading
static <T> List<T> merge(List<T> first, List<T> second) {
List<T> results = new ArrayList<T>();
results.addAll(first);
results.addAll(second);
return results;
} JAVA
#7 operators overloading
static <T> List<T> merge(List<T> first, List<T> second) {
List<T> results = new ArrayList<T>();
results.addAll(first);
results.addAll(second);
return results;
} val results = listOf(1,3) + listOf(2,5)
JAVA
KT
#7 operators overloading
static <T> List<T> merge(List<T> first, List<T> second) {
List<T> results = new ArrayList<T>();
results.addAll(first);
results.addAll(second);
return results;
} val results = listOf(1,3) + listOf(2,5)
JAVA
KT
operator fun <T> Collection<T>.plus(elements: Iterable<T>): List<T>Thank You
Questions?
Kotlin
By Pablo Arqueros
Kotlin
- 91