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

  • Learning Scala Programming
  • Vikash Sharma
  • 152字
  • 2021-06-30 19:07:52

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.

主站蜘蛛池模板: 海淀区| 叙永县| 抚州市| 阳曲县| 嵊州市| 华宁县| 澄城县| 安西县| 乐东| 南和县| 象山县| 尤溪县| 禹州市| 义马市| 枣庄市| 禄劝| 穆棱市| 章丘市| 广饶县| 绩溪县| 来安县| 浮梁县| 临邑县| 肇州县| 图木舒克市| 肇州县| 和政县| 赤壁市| 新巴尔虎左旗| 宜川县| 纳雍县| 射阳县| 乐山市| 库尔勒市| 泰和县| 兴义市| 陵水| 调兵山市| 塔城市| 稷山县| 天台县|