https://kotlinlang.org logo
#announcements
Title
# announcements
i

Iaroslav Postovalov

08/11/2019, 9:35 AM
Hi! When I extend a Java method that has
String[]
in signature Kotlin recommends to have it as
Array<out String>
. It is literally useless because
String
is
final
in Java.
j

James Richardson

08/11/2019, 1:00 PM
You cant change the value of a string, but you can change an array element to contain a different string. In general an array is not a great choice for a parameter type.
k

karelpeeters

08/11/2019, 7:27 PM
That's not relevant though. You're right that it's a useless suggestion, but for general types it's a good idea.
d

Dico

08/11/2019, 10:39 PM
It's a good idea because it makes it so you can't set values on the array. It makes you explicit about the purpose if the parameter. That's why type of
vararg a: String
is also
Array<out String>
I am not sure if compiler will allow setting indices considering String is final. Therefore it isn't useless.
k

karelpeeters

08/11/2019, 10:42 PM
I agree, but a pedantic note: the mutability argument is only half, true, stuff like
reverse()
still works.
j

James Richardson

08/12/2019, 6:37 PM
There's a bit of confusion here. String is final. All good. But String[] allows you to change any index of it to another String. final String[] a = .... ; a[5] = "hello" is perfectly valid. a = .... is not.
1
2 Views