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

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.

主站蜘蛛池模板: 当雄县| 澄迈县| 农安县| 洛浦县| 曲麻莱县| 长白| 邵阳市| 安义县| 富源县| 霍邱县| 贵定县| 镇沅| 嘉定区| 嘉祥县| 平乐县| 中卫市| 敖汉旗| 郁南县| 济源市| 突泉县| 普洱| 东港市| 海伦市| 沙湾县| 阿巴嘎旗| 尚志市| 唐海县| 澳门| 池州市| 崇明县| 定兴县| 武夷山市| 大埔县| 乐都县| 蕲春县| 金湖县| 共和县| 罗田县| 桐城市| 社会| 陕西省|