Explore the new features of Kotlin 1.8.20
Kotlin 1.8.20 has been released and we will explore some new features and improvements.
- We will only cover new language features and standard library updates .
- Please refer to the Resources section for complete details on this release.
language update
Enum class entries
functions
Why is this function needed?
values()
- Returns an array, in most cases we will convert it to a list for manipulation. Arrays have lower performance compared to lists.
enum class Language(val extension: String) {
Kotlin(".KT"),
Java(".java"),
Dart(".dart")
}
// values - Returns Array
val languageValues:Array<Language> = Language.values()
// New function entries - Returns List
val languageEntries:List<Language> = Language.entries
Data objects
- To improve readability, we introduced this new feature.
- It has a neat and clear toString() representation.
object EmployeeObject
data object EmployeeDataObject
// Output
println(EmployeeObject)
println(EmployeeDataObject)
// EmployeeObject@50040f0c
// EmployeeDataObject
Secondary constructors in inline classes can have function bodies
Since version 1.8.20, we can use secondary constructors with function bodies in inline classes.
@JvmInline
value class Employee(private val fullName: String) {
// Allowed since Kotlin 1.4.30:
init {
check(fullName.isNotBlank()) {
"Full name shouldn't be empty"
}
}
// Preview available since Kotlin 1.8.20:
constructor(firstName: String, middleName:String, lastName: String) : this("$firstName $middleName $lastName") {
check(lastName.isNotBlank()) {
"Last name shouldn't be empty"
}
}
}
Standard library updates
Added the Autocloseable interface
For closing resources, interfaces have been added to the common standard library AutoCloseable
.
Also included is an extension function use()
that executes the given block function on the selected resource and then closes it properly whether or not an exception is thrown.
Base64 encoding and decoding
Now in Kotlin we have Base64 support, so Java is no longer needed
There are three types available :
Base64.Default
, Base64.UrlSafe
andBase64.Mime
@OptIn(ExperimentalEncodingApi::class)
fun base64Experimental() {
// Base64.Default
val nameBytes = "Nav".map {
it.code.toByte() }.toByteArray()
val encodedValue = Base64.Default.encode(nameBytes)
// Encode value: TmF2
println("Encoded: $encodedValue")
// Decoded value: Nav
println("Decoded: ${
String(Base64.Default.decode(encodedValue))}")
// Base64.UrlSafe
val googleIOUrlBytes = "google.io".map {
it.code.toByte() }.toByteArray()
// Encode value: Z29vZ2xlLmlv
val encodedURLSafe = Base64.UrlSafe.encode(googleIOUrlBytes)
println("Encoded UrlSafe: $encodedURLSafe")
// Decoded value: google.io
println("Decoded UrlSafe: ${
String(Base64.UrlSafe.decode(encodedURLSafe))}")
}
Support for Kotlin/Native's @Volatile annotation
Prior to 1.8.20, @Volatile
annotations were only available in the public standard library and were valid in the JVM.
class Developer {
@Volatile
private var isAndroidDev: Boolean = false
fun isAndroidDevloper(): Boolean = isAndroidDev
fun setAndroidDev(isAndroidDev: Boolean) {
this.isAndroidDev = isAndroidDev
}
}
Kotlin/JVM
- Preview functionality for Java synthetic property references
public class Developer {
private String name;
private String coreLanguage;
public Developer(String name, String coreLanguage) {
this.name = name;
this.coreLanguage = coreLanguage;
}
public String getName() {
return name;
}
public String getCoreLanguage() {
return coreLanguage;
}
}
- call attribute using reference
val developer = Developer("Nav", "Kotlin")
// references to Java synthetic properties
print("Developer name is ${
developer::name}")
reference
[kotlin1.8.20 new features] https://kotlinlang.org/docs/whatsnew1820.html