I have the following code in PHP public static $bitValueTable = null; and I want to convert it into Kotlin. My variable is a null array in the first step but I add some value after the program runs.
how can I convert?
2 Answers
By default any variable in kotlin can't hold null values but still you can create nullable object using ? operator, for better understanding check below url
https://kotlinlang.org/docs/reference/null-safety.html.
So to create nullable array use below syntax
var myTypeArray: Array<type>? = null // check below example
var myStrArray: Array<String>? = null
Thanks
2 Comments
In kotlin you can use nullable objects with the secure call operator "?".
Now, you have a static variable in PHP, in kotlin there is no "static" as such, however the companion object {} fulfills the same function.
In this way the equivalent of public static $bitValueTable = null;
in kotlin is:
companion object {
var bitValueTable : Array<Type>? = null
}
Obs:
- The default value of the variables in kotlin is public
nullarray? Probably your code could be rewritten in such a way that you don't need it, maybe by using an empty array or by delaying the variable creation until you have a value