Skip to content

Instantly share code, notes, and snippets.

@aoiroaoino
Last active December 17, 2015 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aoiroaoino/5547397 to your computer and use it in GitHub Desktop.
Save aoiroaoino/5547397 to your computer and use it in GitHub Desktop.
val と var
//
// 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