Oktatás * Programozás 2 * Szkriptnyelvek * levelezősök Félévek Linkek * kalendárium |
Scala /
variables, basic types, rich types
Variablesscala> val x = 5 * 6 + 8 // val: constant, cannot change it later x: Int = 38 scala> x = 2 <console>:8: error: reassignment to val x = 2 ^ scala> var y = 7 // var: a variable whose content can change y: Int = 7 scala> y += 1 scala> y res2: Int = 8 scala> In Scala you are encouraged to use If you want, you can specify the type too: scala> val z: String = "hello" // z is a String z: String = hello scala> val width, height = 100 // sets both to 100 width: Int = 100 height: Int = 100 Basic types and rich typesscala> val z: String = "hello" // z is a String z: String = hello scala> :type z // type of a variable String scala> :type 6 + 5 // type of an expression Int Scala has seven numeric types, which are actually classes. Each class has a richer version, which includes lots of convenience methods.
Example: when you write " StringThe convenience methods of String are located in StringOps. Scala uses the String class of Java and adds lots of goodies to it in StringOps. |
Blogjaim, hobbi projektjeim * The Ubuntu Incident [ edit ] |