- Learn Kotlin Programming(Second Edition)
- Stephen Samuel Stefan Bocutiu
- 239字
- 2021-06-24 14:13:37
Operators
Operators are functions that use a symbolic name. In Kotlin, many built-in operators are actually function calls. For example, array access is a real function:
val array = arrayOf(1, 2, 3) val element = array[0]
In this example, the [0] operation is translated into a call to the get(index: Int) function defined on the Array class.
Many operators are predefined in Kotlin, just like they are in most other languages, and most operators tend to be combined with the infix style. This is immediately familiar in the guise of binary operators on numbers.
Often operators are preferred over real names if the operators are already familiar to the users. In fields such as mathematics or physics, where operators are routinely used, it would be natural to also use operations in code where appropriate. For example, in the case of matrices, using the + character for matrix addition feels more natural than using the words add or plus. It is also easier to read when the parentheses are omitted:
val m: Matrix = val n: Matrix = val p = n + q
As you can see, creating a new p matrix by adding together two other matrices, n and m, by using the + syntax is succinct and readable.
- Mastering Entity Framework Core 2.0
- Learning NServiceBus(Second Edition)
- Spring 5企業(yè)級(jí)開發(fā)實(shí)戰(zhàn)
- R語言經(jīng)典實(shí)例(原書第2版)
- Learning Real-time Processing with Spark Streaming
- Python數(shù)據(jù)可視化:基于Bokeh的可視化繪圖
- 算法訓(xùn)練營:入門篇(全彩版)
- jQuery從入門到精通 (軟件開發(fā)視頻大講堂)
- 秒懂設(shè)計(jì)模式
- Data Analysis with Stata
- C#程序設(shè)計(jì)
- Extreme C
- Java7程序設(shè)計(jì)入門經(jīng)典
- 零基礎(chǔ)學(xué)HTML+CSS第2版
- 進(jìn)入IT企業(yè)必讀的324個(gè)Java面試題