Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin as a Modernized Java @ Oracle Code One 2018

Kotlin as a Modernized Java @ Oracle Code One 2018

Kotlin was designed with Java developers in mind. Java is evolving, but the language evolution is mostly limited to adding new features. It is nearly impossible to remove features that have proved to be troublesome from a language as popular as Java. This session looks at those features that have been either removed or radically adjusted in Kotlin and how this leads to better, modern, easier-to-read code in Kotlin while still retaining the ability to leverage all the Java libraries in the Java ecosystem.

Roman Elizarov

October 25, 2018
Tweet

More Decks by Roman Elizarov

Other Decks in Programming

Transcript

  1. Kotlin as a Modernized Java Programming in XXI Century Presented

    at JavaOne, 2018 email elizarov@ Roman Elizarov @relizarov
  2. Error handling int res = fread(buf, n, file); if (res

    < n) { // error or EOF if (ferror(file) != 0) { // handle error } return; }
  3. Error handling int res = fread(buf, n, file); if (res

    < n) { … } res = fread(buf, m, file); if (res < n) { // error or EOF if (ferror(file) != 0) { // handle error } return; }
  4. Exceptions try { FileInputStream file = new FileInputStream(fileName); // …

    int res = file.read(buf, 0, n); // throws exception // … } catch (IOException e) { // handle error }
  5. Exceptions try { FileInputStream file = new FileInputStream(fileName); // …

    int res = file.read(buf, 0, n); // … res = file.read(buf, 0, m); // throws exception // … } catch (IOException e) { // handle error }
  6. Checked Exceptions Data work(String fileName) throws IOException { FileInputStream file

    = new FileInputStream(fileName); // … int res = file.read(buf, 0, n); // … res = file.read(buf, 0, m); // … }
  7. Imperative List<Result> results = new ArrayList<>(); for (String fileName :

    fileNames) { Result work = work(fileName); results.add(work); }
  8. Functional + Checked Exceptions = ! List<Result> results = fileNames.stream()

    .map(fileName -> work(fileName)) .collect(Collectors.toList());
  9. Functional in Java as an afterthought List<Result> results = fileNames.stream()

    .map(fileName -> work(fileName)) .collect(Collectors.toList());
  10. val results = fileNames .map { work(it) } .filter {

    it.isGood() } .doMyProcessing()
  11. val results = fileNames .map { work(it) } .filter {

    it.isGood() } .doMyProcessing()
  12. val results = fileNames .map { work(it) } .filter {

    it.isGood() } .doMyProcessing()
  13. Kotlin extension functions val results = fileNames .map { work(it)

    } .filter { it.isGood() } .doMyProcessing() fun List<Data>.doMyProcessing() = …
  14. Kotlin top-level functions val results = fileNames .map { work(it)

    } .filter { it.isGood() } .doMyProcessing() fun List<Data>.doMyProcessing() = …
  15. Before type inference int count = … double average =

    … List<String> strings = … Map<Warehouse, List<OrderItem>> items = …
  16. With type inference var count = … var average =

    … var strings = … var items = …
  17. With type inference var count = … var average =

    … var strings = … Map<Warehouse, List<OrderItem>> items = …
  18. Types on right! var count = … var average =

    … var strings = … var items: Map<Warehouse, List<OrderItem>> = …
  19. If Java was designed in 201x It would have been

    designed for type inference with types on the right
  20. Collections in Kotlin are read-only fun process(input: List<Data>): Int {

    input.removeIf { data -> !data.isGood() } return input.size }
  21. How type-safe is that? int process(List<Data> input) { … int

    size = input.size(); … return size; }
  22. How type-safe is that? int process(List<Data> input) { … final

    int size = input.size(); … return size; }
  23. Kotlin: Make val not var fun process(input: List<Data>): Int {

    … val size = input.size … return size }
  24. What if Java was designed in 201x? ØNo checked exceptions

    ØDesigned for type inference ØFunctional by design ØNo exposed primitives ØNull safety ØWell-founded defaults ØOptional semicolon
  25. Properties in Java public class Person { private String firstName;

    private String lastName; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } …
  26. Primary constructor in Java public class Person { private String

    firstName; private String lastName; public Person(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } …
  27. Equals/hashCode/etc in Java public class Person { private String firstName;

    private String lastName; @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Person)) return false; Person person = (Person) o; return Objects.equals(firstName, person.firstName) && Objects.equals(lastName, person.lastName); } …
  28. String concatenation in Java public class Person { @Override public

    String toString() { return firstName + " " + lastName; } …
  29. String format in Java public class Person { @Override public

    String toString() { return String.format("%s %s", firstName, lastName); } …
  30. Singleton in Java public class Singleton { public static final

    INSTANCE = new Singleton(); private Singleton() {} … }
  31. Kotlin ØInteroperability with JVM ecosystem ØEasy to learn for Java

    developers by design ØExtensible, DSL-friendly ØPragmatic ØFun to work with