Scalaのお勉強ってことで、引き続きドワンゴの研修資料を読んで勉強していく。
今日はScalaの基本の章を。
基本的には章の構成のとおりに進めていく感じです。
REPLでScalaを触ってみる
Hellow, World!
前回の記事でIntellij IDEA上でのHello World!は実行したが、今回はsbt console上で実行してみる
scala> println("Hello, World!) Hello, World
printlnを外してみると、、
scala> "Hello, World!!" res0: String = Hello, World!!
なるほど、式の評価の結果がString
型になっている、と。
練習問題
ここからは練習問題。以下のリテラルをsbt consoleで出力してみる。
- 0xff
scala> 0xff res1: Int = 255
これは普通にInt
型。
- 1e308
scala> 1e308 res2: Double = 1.0E308
これも普通にDouble
型
- 9223372036854775807L
- 9223372036854775808L
scala> 9223372036854775807L res3: Long = 9223372036854775807
scala> 9223372036854775808L <console>:1: error: integer number too large 9223372036854775808L ^
なるほど、LをつけたのでLong
型になる。そして最大値。
なので+1
すると、範囲を超えてエラーになる、、、と。エラーになるのはちょっと新鮮。
- 9223372036854775807
scala> 9223372036854775807 <console>:1: error: integer number too large 9223372036854775807 ^
こちらはL無しなのでInt
型として扱われるが、最大値を超えているのでエラーになる。
- 922337203685477580.7
scala> 922337203685477580.7 res4: Double = 9.2233720368547763E17
大きい数字、かつ小数の値なのでDouble
型。
- 1.00000000000000000001 == 1
scala> 1.00000000000000000001 == 1 res5: Boolean = true
==
で比較ができる、かつ精度の関係でtrue
判定になる。
- "\u3042"
- "\ud842\udf9f"
scala> "\u3042" res6: String = あ
scala> "\ud842\udf9f" res7: String = 𠮟
2byte文字も扱える。
と、ここまでのことから明示的な型の指定はなく推測がされるということもついでにわかった。
簡単な計算
引き続き、REPLで試していく。まずは足し算から。
scala> 1 + 2 res8: Int = 3
整数同士の足し算なので、結果もInt
型が返ってくる。
他のプログラム言語と同様に+
, -
, *
, /
, %
がInt
型で使用可能↓
Int型
scala> 2 * 2 res11: Int = 4 scala> 4 / 2 res12: Int = 2 scala> 4 % 3 res13: Int = 1
Double型
同じことは浮動小数点型でも可能。
scala> 1 + 2 res16: Int = 3 scala> 2.2 * 2 res17: Double = 4.4 scala> 4.5 / 2 res18: Double = 2.25 scala> 4.0 % 2.0 res19: Double = 0.0 scala> 4.0 % 3.0 res20: Double = 1.0 scala> 4.0 % 2.1 res21: Double = 1.9 scala> (4.5).asInstanceOf[Int] res22: Int = 4 scala> (4.0).asInstanceOf[Int] res23: Int = 4
Double型の変数にメソッドがついててその中の一つasInstanceOfを使ってキャスト??
Double型の、、ではなくAny型のメソッドとのこと。それで調べ直した。
練習問題
再度、ここからは練習問題。
この辺の感覚は他のプログラミング言語と一緒。
- 2147483647 + 1
scala> 2147483647 + 1 res0: Int = -2147483648
Int
型とInt
型なので結果もInt
型
- 9223372036854775807L + 1
scala> 9223372036854775807L + 1 res1: Long = -9223372036854775808
Long
型とInt
型なのでLong
にアップキャスト(Scala的にこういうのかは知らないが。。)。
- 1e308 + 1
scala> 1e308 + 1 res2: Double = 1.0E308
Double
型とInt
型でDouble
型。
- 1 + 0.0000000000000000001
scala> 1 + 0.0000000000000000001 res3: Double = 1.0
Double
型とDouble
型の加算だけど丸め誤差の関係で1.0
になる
- 1 - 1
scala> 1 - 1 res4: Int = 0
Int
型同士の減算なのでInt
型
- 1 - 0.1
scala> 1 - 0.1 res5: Double = 0.9
Double
型とInt
型でDouble
型。
- 0.1 - 1
scala> 0.1 - 1 res6: Double = -0.9
Double
型とInt
型でDouble
型。
- 0.1 - 0.1
scala> 0.1 - 0.1 res7: Double = 0.0
Double
型同士なのでDouble
型の0になる。
- 0.0000000000000000001 - 1
scala> 0.0000000000000000001 - 1 res8: Double = -1.0
- 0.1 * 0.1
scala> 0.1 * 0.1 res9: Double = 0.010000000000000002
Double
型とDouble
型の減算+誤差で1.0
になる
- 20 * 0.1
scala> 20 * 0.1 res10: Double = 2.0
Double
型同士の乗算でDouble
型
- 1 / 3
scala> 1 / 3 res11: Int = 0
Int
型同士の除算で小数になるので結果が0
- 1.0 / 3
scala> 1.0 / 3 res12: Double = 0.3333333333333333
こっちは1.0
とDouble
型を明示したので結果がDouble
型になる。
- 1 / 3.0
scala> 1 / 3.0 res13: Double = 0.3333333333333333
こっちも同様3.0
とDouble
型を明示したので結果がDouble
型になる。
- 3.0 / 3.0
scala> 3.0 / 3.0 res14: Double = 1.0
Double
型同士の除算
- 1.0 / 10 * 1 / 10
scala> 1.0 / 10 * 1 / 10 res15: Double = 0.01
1.0 / 10
の結果がDouble
型なので結果も正常に計算できてDouble
型の値が得られる。
- 1 / 10 * 1 / 10.0
scala> 1 / 10 * 1 / 10.0 res16: Double = 0.0
こちらは1 / 10
の結果がInt
型で0
になり、その結果と後続の式が計算されるので結果が0
になる。
ということで、今日はここまで。