- 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.
推薦閱讀
- Clojure Programming Cookbook
- PostgreSQL Cookbook
- Django開發從入門到實踐
- Three.js開發指南:基于WebGL和HTML5在網頁上渲染3D圖形和動畫(原書第3版)
- Reactive Programming with Swift
- Visual Basic程序設計(第3版):學習指導與練習
- Mastering C# Concurrency
- 老“碼”識途
- WordPress Plugin Development Cookbook(Second Edition)
- Learning JavaScript Data Structures and Algorithms
- C語言程序設計
- 區塊鏈架構之美:從比特幣、以太坊、超級賬本看區塊鏈架構設計
- 零基礎學HTML+CSS第2版
- Ionic3與CodePush初探:支持跨平臺與熱更新的App開發技術
- 零基礎學Java(第5版)