eavur.blogg.se

Kotlin type any
Kotlin type any











kotlin type any kotlin type any

However, in Kotlin, we can use the safe cast operator as? to achieve it. In Java, we can do that by catching the ClassCastException and returning null. Instead, we would like to have a null value. Sometimes, if the type casting fails, we don’t want the function to throw an exception. Also, we use Kotlin’s assertFailsWith function to verify if the ClassCastException is thrown. Next, let’s test it: val aString: Any = "I am a String"ĪssertThat(unsafeCastToString(aString)).isEqualTo(aString)Īs the test above shows, we pass a String and then a Long to the function. Println(book.Fun unsafeCastToString(obj: Any): String = obj as String

kotlin type any

data class Book(val name: String, val authorName: String = "Anupam", val lastModified: Long = 1234567, val rating: Float = 5f, val downloads: Int = 1000) N is the number of parameters in the constructor. Destructuring DeclarationsĬomponentN() function lets us access each of the arguments specified in the constructor, in the order specified. Note: The equals() method is equivalent to = in kotlin. The first and third object hashcodes are equal hence they are equal. Println("newBook and book are NOT equal") Let’s see how a Book.java POJO class looks like: public class Book ") Kotlin Data Classĭo you get tired of writing thousands of lines of code for your POJO data classes in Java? Every Java Programmer at some stage must have taken a note of the number of lines of code they need to write for classes that just need to store some data. If you haven’t read the Kotlin Classes post, we recommend you to do so before proceeding. In this tutorial, we’ll look at Kotlin Data Class.













Kotlin type any