Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write Java brackets "[ ]" in Kotlin?

I apologize in advance for my programming political incorrectness, as I do not know what the thing I need to type is called.

I have this Java code.

FrutasVerduras frutasVerduras_datos[] = new FrutasVerduras[] {

}

How would this be written in Kotlin?

like image 883
StealthDroid Avatar asked Feb 13 '26 22:02

StealthDroid


1 Answers

That thing is called an array. Currently, in Kotlin you can't create them using the same syntax as in Java. Though that's going to change (see "Language feature: array literals in annotations") a little in the near future. The Kotlin way to do it is using arrayOf(...). Keep in mind though that for basic types you should use the adequate function (e.g. for Int use intArrayOf) as otherwise the JVM will create an array of Integers (object) instead of int (primitive) causing 12 (?) bytes more per element

like image 200
Mibac Avatar answered Feb 15 '26 11:02

Mibac



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!