1

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
  • Why do you need a null array? 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 Commented May 26, 2019 at 13:13
  • I am trying to convert one PHP project to Kotlin and I want to be everything the same in both. this array depends on users and is used in multi-place so I don't know when it creates. Commented May 26, 2019 at 19:55

2 Answers 2

1

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

Kotlin Arrays Documentation

Thanks

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

2 Comments

thanks for answer but there is a static keyword. how can I handle that?
Strictly speaking, there are no static members in Kotlin. Instead, a companion object should be used.
0

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

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.