dasuma
dasuma

Reputation: 791

What's difference between get and [] in Kotlin?

I currently have an element

val test: map <int, List <string>

I have a question which is the best way to obtain an element if

test.get (100) or test [100]

What is the difference, which has the best performance?

Upvotes: 1

Views: 675

Answers (1)

Patric
Patric

Reputation: 1627

It is the exact same thing. In Kotlin, you can override operators. [] is the get operator, so the resulting jvm byte code will be exactly alike.

You can do the same thing with other operators, e.g. plus:

val x = 3 + 2

is the same thing as

val x = 3.plus(2)

If you are using an IDE like IntelliJ, you can CTRL-click on the [] or the + operator and on the get() and plus() function respectively and you will see that you end up at the same place.

Upvotes: 3

Related Questions