Why you should starting programming in Kotlin for Android
Some might think that Kotlin is new.
I will demonstrate Kotlin history, who is behind it, and why is it a must to learn.
With some links and good references
This slides were conducted in GDG 6 October Google i/o event
8. Why Should I care!!?
• Statically typed programming language targeting the JVM
• Support for functional and OO paradigms
• Pragmatic, safe, concise, great Java interop
• Free and open-source
• Drastically reduce the amount of boilerplate code
• Lambda expression
• Avoid entire classes of errors such as null pointer exceptions
• Java 9 is …
8
9. The Strengths of Kotlin
• Modeling the data of your application concisely and expressively
• Creating reusable abstractions using functional programming
techniques
• Creating expressive domain-specific languages
• Java interop ensures that all existing Java frameworks can be used
• No rewrite required to start using Kotlin in existing codebase
• Existing investment is fully preserved
9
10. Java Model Example
10
public class Customer {
private String name;
private String email;
private String company;
public Customer(String name) {
this(name, "", "");
}
public Customer(String name, String email) {
this(name, email, "");
}
public Customer(String name, String email, String company) {
this.name = name;
this.email = email;
this.company = company;
}
11. Java Model Example, cont.
11
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
12. Java Model Example, cont.
12
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Customer customer = (Customer) o;
if (name != null ? !name.equals(customer.name) : customer.name != null) return false;
if (email != null ? !email.equals(customer.email) : customer.email != null) return false;
return company != null ? company.equals(customer.company) : customer.company == null;
}
13. Java Model Example, cont.
13
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (email != null ? email.hashCode() : 0);
result = 31 * result + (company != null ? company.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "Customer{" +
"name='" + name + ''' +
", email='" + email + ''' +
", company='" + company + ''' +
'}';
}
}
14. Kotlin Model Example, cont.
14
data class Customer(var name: String,
var email: String = "",
var company: String = "")