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

JVM languages shootout - Java, Scala & *Kotlin*

oshai
September 23, 2019

JVM languages shootout - Java, Scala & *Kotlin*

Java is probably the most popular statically typed language for years.
In the last decade, there has been a significant rise of other languages running on the Java Virtual Machine.
In this talk, I will compare three of the most popular JVM languages: Java, Scala, and Kotlin. We will see where each language shines and some of the advantages of each of them.
We will also take a deeper look of Kotlin - the (relatively) new kid on the block.

oshai

September 23, 2019
Tweet

More Decks by oshai

Other Decks in Technology

Transcript

  1. Boilerplate In computer programming, boilerplate code or boilerplate refers to

    sections of code that have to be included in many places with little or no alteration. It is often used when referring to languages that are considered verbose, i.e. the programmer must write a lot of code to do minimal jobs.
  2. IDE

  3. Hello Kotlin! Java public class Hello { public static void

    main(String[] args) { System.out.println("Hello!"); } } Kotlin fun main() { println("Hello!") }
  4. POJO Class Java public class AdCpcData { private int adId;

    private double cpc; public AdCpcData(final int adId, final double cpc) { this.adId = adId; this.cpc = cpc; } public double getCpc() { return cpc; } public int getAdId() { return adId; } public void setAdId(final int adId) { this.adId = adId; } public void setCpc(final double cpc) { this.cpc = cpc; } }
  5. POJO Class Java public class AdCpcData { private int adId;

    private double cpc; public AdCpcData(final int adId, final double cpc) { this.adId = adId; this.cpc = cpc; } public double getCpc() { return cpc; } public int getAdId() { return adId; } public void setAdId(final int adId) { this.adId = adId; } public void setCpc(final double cpc) { this.cpc = cpc; } } equals(), hashcode(), toString()…
  6. POJO Class Java public class AdCpcData { private int adId;

    private double cpc; public AdCpcData(final int adId, final double cpc) { this.adId = adId; this.cpc = cpc; } public double getCpc() { return cpc; } public int getAdId() { return adId; } public void setAdId(final int adId) { this.adId = adId; } public void setCpc(final double cpc) { this.cpc = cpc; } @Override public String toString() { return com.google.common.base.Objects.toStringHelper(this) .add("adId", adId) .add("cpc", cpc) .toString(); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; AdCpcData adCpcData = (AdCpcData) o; return adId == adCpcData.adId && Double.compare(adCpcData.cpc, cpc) == 0; } @Override public int hashCode() { return Objects.hash(adId, cpc); } } equals(), hashcode(), toString()…
  7. POJO Class Java public class AdCpcData { private int adId;

    private double cpc; public AdCpcData(final int adId, final double cpc) { this.adId = adId; this.cpc = cpc; } public double getCpc() { return cpc; } public int getAdId() { return adId; } public void setAdId(final int adId) { this.adId = adId; } public void setCpc(final double cpc) { this.cpc = cpc; } @Override public String toString() { return com.google.common.base.Objects.toStringHelper(this) .add("adId", adId) .add("cpc", cpc) .toString(); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; AdCpcData adCpcData = (AdCpcData) o; return adId == adCpcData.adId && Double.compare(adCpcData.cpc, cpc) == 0; } @Override public int hashCode() { return Objects.hash(adId, cpc); } } equals(), hashcode(), toString()… 57 LOC
  8. Data Class Java public class AdCpcData { private int adId;

    private double cpc; public AdCpcData(final int adId, final double cpc) { this.adId = adId; this.cpc = cpc; } public double getCpc() { return cpc; } public int getAdId() { return adId; } public void setAdId(final int adId) { this.adId = adId; } public void setCpc(final double cpc) { this.cpc = cpc; } @Override public String toString() { return com.google.common.base.Objects.toStringHelper(this) .add("adId", adId) .add("cpc", cpc) .toString(); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; AdCpcData adCpcData = (AdCpcData) o; return adId == adCpcData.adId && Double.compare(adCpcData.cpc, cpc) == 0; } @Override public int hashCode() { return Objects.hash(adId, cpc); } } Kotlin data class AdCpcData( var adId: Int, var cpc: Double )
  9. Variable declaration Java private final Map<String, String> myMap = new

    HashMap<>(); myMap.put("Hi", "There"); Kotlin val myMap = mapOf("Hi" to "There")
  10. What’s wrong with Scala? • Readability sucks. Examples: – Collections 

    // Kotlin ^^^ fun <T, R> map(transform: (T) -> R): List<R>
  11. What’s wrong with Scala? • Readability sucks. Examples: – Collections 

    def map[B, That](f: A => B) (implicit bf: CanBuildFrom[Repr, B, That]): That
  12. What’s wrong with Scala? • Readability sucks. Examples: – Collections

    – Implicit conversions – Operator overloading
  13. What’s wrong with Scala? • Readability sucks. Examples: – Collections

    – Implicit conversions – Operator overloading – Underscore 
  14. What’s wrong with Scala? • Compilation time > mvn clean

    install
 … [INFO] Module................................................. SUCCESS [ 7.115 s]
 [INFO] Module................................................. SUCCESS [ 32.680 s]
 [INFO] Module................................................. SUCCESS [01:05 min]
 [INFO] Module................................................. SUCCESS [ 1.080 s]
 [INFO] Module................................................. SUCCESS [ 44.077 s]
 [INFO] Module................................................. SUCCESS [ 1.234 s]
 [INFO] Module................................................. SUCCESS [ 3.410 s]
 [INFO] Module................................................. SUCCESS [ 26.661 s]
 [INFO] Module................................................. SUCCESS [ 45.317 s]
 [INFO] Module................................................. SUCCESS [ 46.435 s]
 [INFO] Module................................................. SUCCESS [ 37.738 s]
 [INFO] Module................................................. SUCCESS [ 14.198 s]
 [INFO] ------------------------------------------------------------------------
 [INFO] BUILD SUCCESS
 [INFO] ------------------------------------------------------------------------
 [INFO] Total time: 06:37 min (Wall Clock)
 [INFO] Finished at: 2017-07-02T15:13:48+03:00
 [INFO] Final Memory: 970M/2498M
 [INFO] ------------------------------------------------------------------------ >
  15. What’s wrong with Scala? • Compilation time > mvn clean

    install | grep min 
 [INFO] Scala............................................... SUCCESS [01:05 min]
 [INFO] Scala............................................... SUCCESS [01:30 min]
 [INFO] Scala............................................... SUCCESS [02:29 min]
 [INFO] Scala............................................... SUCCESS [01:16 min]
 [INFO] Scala............................................... SUCCESS [01:17 min]
 [INFO] Scala............................................... SUCCESS [01:16 min]
 [INFO] Scala............................................... SUCCESS [01:25 min]
 [INFO] Scala............................................... SUCCESS [01:22 min]
 [INFO] Scala............................................... SUCCESS [02:21 min]
 [INFO] Scala............................................... SUCCESS [01:05 min]
 [INFO] Scala............................................... SUCCESS [01:23 min]
 [INFO] Scala............................................... SUCCESS [01:15 min]
 [INFO] Scala............................................... SUCCESS [01:28 min]
 [INFO] Scala............................................... SUCCESS [01:13 min]
 [INFO] Scala............................................... SUCCESS [01:11 min]
 [INFO] Scala............................................... SUCCESS [01:05 min]
 [INFO] Total time: 06:37 min (Wall Clock) >
  16. • (Popular) functional programming on the JVM • Many advanced

    features There must be also something good about Scala?
  17. • We saw some statistics • We learned about boilerplate

    • We saw some Scala pain points Recap
  18. Collections val fruits = listOf("banana", "avocado", "apple", "kiwi") fruits .filter

    { it.startsWith("a") } .sortedBy { it } .map { it.toUpperCase() } .forEach { println(it) }
  19. Testing Java @Test public void testShouldProcessCampaign_WhenFlyToInRange() throws Exception { …

    } Kotlin @Test fun `when "fly to" is in range - should process the campaign`() { TODO() }
  20. • Low entry barriers • Verbose • Popular • Old

    (but progressing) • Modern • Pragmatic • Inter- operable • FP in the JVM • Advanced features