Last active
December 17, 2015 03:59
-
-
Save aoiroaoino/5547397 to your computer and use it in GitHub Desktop.
val と var
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Scalaの変数宣言は二種類あります。 | |
// | |
val name: String = "Aoino" | |
var num: Int = 5 | |
// | |
// valは再代入不可な変数です。(value) | |
// | |
scala> val name = "Aoino" | |
name: java.lang.String = Aoino | |
scala> name = "Hoge" | |
<console>:8: error: reassignment to val | |
name = "Hoge" | |
^ | |
// | |
// varは歳代入可能な変数です。(variable) | |
// | |
scala> var num = 5 | |
num: Int = 5 | |
scala> num = 2 | |
num: Int = 2 | |
scala> num | |
res0: Int = 2 | |
// | |
// 型推論により、型を書かなくても宣言出来ます。 | |
// | |
val name = "Aoino" | |
var num = 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment