Blog Infos
Kotlin 1.8.20 has been released, and we will explore some of the new features/improvements.
- We will cover only new language features and Standard library updates.
- Please refer to the References section to check the full details of this release
New language features
Enum class entries function
Why do we need this new function?
values()
— ReturnsArray
and most of the time we convert it to alist
to work with it. Also as compared toLists
,Arrays
are less performant.- Please check here to learn more about the performance issues.
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
- For better readability purposes, We got this new feature 🎉
- It has a neat and clean
toString()
representation
object EmployeeObject data object EmployeeDataObject // Output println(EmployeeObject) println(EmployeeDataObject) // EmployeeObject@50040f0c // EmployeeDataObject 💜
Secondary constructors with the body in inline classes
- Now starting with 1.8.20, We can use
Secondary constructors with bodies
ininline 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
Autocloseable interface
- To close resources, the
AutoCloseable interface
has been added to the common standard library. - Extension function
use()
is also included, which executes a given block function on the selected resource and then closes it down properly, whether anexception is thrown or not
.
Base64 encoding and decoding
Now we have
Base64 support in
Kotlin. So no more Java 😃 🎉
3 types are available
Base64.Default,
Base64.UrlSafe
Base64.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))}") }
Job Offers
Support for
@Volatile annotation in Kotlin/Native
Before 1.8.20 this annotation in only available in common standard library and effective in JVM
- Check here to learn more about
@Volatile annotation
class Developer { @Volatile private var isAndroidDev: Boolean = false fun isAndroidDevloper(): Boolean = isAndroidDev fun setAndroidDev(isAndroidDev: Boolean) { this.isAndroidDev = isAndroidDev } }
Kotlin/JVM
Preview of 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; } }
- Accessing properties using references
val developer = Developer("Nav", "Kotlin") // references to Java synthetic properties print("Developer name is ${developer::name}")
References
Kotlin 1.8.20 has been released, and we will explore some of the new features/improvements. We will cover only new language features and Standard library updates. Please refer to the References section to check the full details of this release
Stay in touch
Nav Singh (@navczydev@androiddev.social) — Android Dev Social
Nav Singh(ਨਵ ਸਿੰਘ)☬ (@navczydev) / Twitter
Nav Singh (ਨਵ ਸਿੰਘ) ☬🇨🇦 | LinkedIn
navczydev (Nav Singh) · GitHub
This article was previously published on proandroiddev.com