Blog Infos
Author
Published
Topics
Author
Published
Topics

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() — Returns Array and most of the time we convert it to a list to work with it. Also as compared to ListsArrays 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 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
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 an exception 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

Job Offers

There are currently no vacancies.

OUR VIDEO RECOMMENDATION

Jobs

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

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
It’s one of the common UX across apps to provide swipe to dismiss so…
READ MORE
blog
Hi, today I come to you with a quick tip on how to update…
READ MORE
blog
Automation is a key point of Software Testing once it make possible to reproduce…
READ MORE
blog
Drag and Drop reordering in Recyclerview can be achieved with ItemTouchHelper (checkout implementation reference).…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu