Hi! When I extend a Java method that has `String[]...
# announcements
i
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
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
That's not relevant though. You're right that it's a useless suggestion, but for general types it's a good idea.
d
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
I agree, but a pedantic note: the mutability argument is only half, true, stuff like
reverse()
still works.
j
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