0

*just for learning purpose

In Kotlin we can create an array to hold multiple data types including null as bellow

  var multiDataArray = arrayOf(10, "Book", 10.99, 'A', null)

But if we try to create an Array with Any type in either way bellow

val values: Array<Any> = arrayOfNulls(items.size)
val values: Array<Any> = Array<Any>(items.size){null}

it is not allowed compiler with the error of 'Kotlin: Type mismatch: inferred type is Array<Any?> but Array<Any> was expected' it is suggesting to use val values: Array<Any?>

Question 1. What's the reason for that? it sounds like for null safety, indicating the compiler the possibility of null?

What if I want an array that I guarantee for no null values. do I still need to define it as Array<Any?> like bellow as below.

//This one will give the compile error
fun arrayTest0(items: Array<Any>): Array<Any> {
    val values: Array<Any> = arrayOfNulls(items.size)
    var i = 0;
    items.forEach { num -> values[i++] = num }

    return values;
}

//The working one
fun arrayTest1(items: Array<Any?>): Array<Any?> {
    val values: Array<Any?> = arrayOfNulls(items.size)
    var i = 0;
    items.forEach { num -> values[i++] = num }

    return values;
}
1
  • 1
    Yes, Any means nulls are disallowed. Any? allows null values. You really should read official docs: kotlinlang.org/docs/null-safety.html . Commented Sep 12, 2021 at 8:19

1 Answer 1

2

What's the reason for that? it sounds like for null safety, indicating the compiler the possibility of null?

You are right about this, It is indicating about the null safety . Since you are returning arrayofnulls and the return type for the same is Array of type Any (which in Kotlin Terms means the data is not null), it has to made sure that it is not null , else it may end up crashing the code in the future if it contains any null values within it .While making a program for production , such safety measure's are too helpful and save you from lot of errors and crashes. Kotlin language has it's uphand over other languages due to this null safety feature it possess.

What if I want an array that I guarantee for no null values. do I still need to define it as Array<Any?>

No , if you assure that there is no null value . You can make use of !! operator , while returning , even if the compiler is asking you for null-safety check . The aim of this !! operator is that the programmer is sure that the code is not going to return null and hence can override the ?.(null safety). While returning a non-null object , you do not have a need to specify ?. in your return type. You can return it , how it is done the normal way without null safety operators .

The compiler is warning you in the below function is because you want to return a non-null type of array . But what are you returning is an arrayofnull , which might crash your code in your further useCase, so Kotlin makes use of null-safety to avoid errors .

fun arrayTest0(items: Array<Any>): Array<Any> {
    val values: Array<Any> = arrayOfNulls(items.size)
    var i = 0;
    items.forEach { num -> values[i++] = num }

    return values;
}

Refer the documentation for more details and examples :

https://kotlinlang.org/docs/null-safety.html#nullable-types-and-non-null-types

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.