官术网_书友最值得收藏!

Arithmetic operators

We can perform arithmetic operations using the arithmetic operators. Arithmetic operators contain operators for addition (+), subtraction (-), multiplication (*), division (/), and remainder (%). We've already seen many examples for addition, not mentioning that these operators are methods!

Let's take examples of others:

scala> val x = 10 - 1
x: Int = 9

scala> val y = 10 * 1
y: Int = 10

scala> val z = 10 / 1
z: Int = 10

scala> val yx = 10 % 9
yx: Int = 1

These operators have their overloaded versions also defined, to see that we may try with different types as operands. Let's take an Int and Double:

scala> val yx = 10 % 9.0
yx: Double = 1.0

Here, the first operand is an Int, and the second one is a Double, and since Int is view-bounded to Double, the result is converted to Double.

主站蜘蛛池模板: 杂多县| 和静县| 石台县| 晋江市| 罗甸县| 莫力| 东乌珠穆沁旗| 博兴县| 乳山市| 横峰县| 都昌县| 寿宁县| 乾安县| 右玉县| 巴东县| 瓦房店市| 克拉玛依市| 班玛县| 郓城县| 昂仁县| 齐齐哈尔市| 岗巴县| 武义县| 博爱县| 靖远县| 淮北市| 民县| 茶陵县| 中西区| 华坪县| 宣城市| 石城县| 错那县| 九江县| 开江县| 昭苏县| 深州市| 洮南市| 嘉祥县| 霞浦县| 绥中县|