Skip to content

Instantly share code, notes, and snippets.

@makosblade
Created January 18, 2022 22:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makosblade/8fb1f577d81f02025784093d25ec1303 to your computer and use it in GitHub Desktop.
Save makosblade/8fb1f577d81f02025784093d25ec1303 to your computer and use it in GitHub Desktop.
Java 17 vs Kotlin

Java Kotlin Tooling Comparison

-- Java Kotlin
Build/Run/Dist Gradle Gradle
Testing Junit Junit
Code coverage jacoco jacoco
Static analysis PMD Detekt, CPD
Code linting checkstyle ktlint
Mocking Mockito MockK
Bytecode tools infer spotbugs/findbugs

Why use Kotlin over Java 17

  • Type system superiority
    • Nullability built in, default non-null (arg: String is non-null, arg: String? is nullable)
    • No raw types (List must have a type specified, even Any?)
    • Array invariance (Can't assign Array<Int> to Array<Any> without a type projection [below])
    • Function types (Don't require an interface class for more than 2-arg lambdas)
    • Declaration-site variance and type projections [kotlin variance] [variance explained]
      • Java only has use-site type variance but has wanted to add declaration-site variance for some time [link]
      • Kotlin type projections provide use-site variance
  • Mutability as a language construct
    • Built-in to allocation syntax, var mutable, val immutable
    • Kotlin standard library separates mutable and immutable object APIs as well (listOf(1,2,3) returns ImmutableList, mutableListOf(1,2,3) returns MutableList)
  • Coroutines
  • Code length
  • Functional constructs
    • Function types (above)
    • Inline functions (let the compiler inline your lambda)
    • Extension functions (extend third-party classes with new functionality)
    • Trailing lambdas
  • Singleton objects, lazily initialized and thread-safe
  • Data classes (Java has record classes but Data classes are much more flexible)
  • Syntactic enhancements
    • lazy keyword to lazily initialize anything
    • function default values
    • named parameters
    • range expressions
    • string templates
    • multiline string (now in java)
    • smart casts built into the compiler
    • lateinit keyword
    • property and interface delegates using the by keyword
    • typealias keyword
    • infix keyword for infix notation (1 plus 2 by infix fun Int.plus(x: Int))
    • tailrec keyword to label tail recursive functions for loop conversion
    • constructor hierarchy enforced by the compiler (must have all n-ary constructors call through to the primary constructor)
  • Generates JVM bytecode (interoperable with just about any library that also generates JVM bytecode)

Why use Java 17 over Kotlin

  • Mature compilation constructs (slightly faster language compilation)
  • More mature tooling around java projects (PMD, jmh, etc.)
  • Ternary operator
  • Static notation is more straightforward than Kotlin versions
  • Dedicated primitive types are more intuitive than using boxed classes (though they are optimized down to actual primitives where possible in Kotlin)
  • Checked Exceptions
@hatahet
Copy link

hatahet commented Oct 30, 2022

string templates

Coming to Java, in an arguably better way (can implement custom templates): https://openjdk.org/jeps/430

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment