Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

* Programming 1 (BI)
  ◇ exercises
  ◇ quick link

* Scripting Languages
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* aktuális (2023/24/2)
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Scala


[ edit | logout ]
[ sandbox | passwd ]

variables, basic types, rich types

#############################

Variables

scala> 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 vals instead of vars.

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 types

scala> 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.

normal rich
Byte RichByte
Char RichChar
Short RichShort
Int RichInt
Long RichLong
Float RichFloat
Double RichDouble
Boolean RichBoolean

Example: when you write "1 to 10", it is a shorthand for "1.to(10)", i.e. an integer has a method called "to". How? This feature is added in the RichInt class.

String

The convenience methods of String are located in StringOps. Scala uses the String class of Java and adds lots of goodies to it in StringOps.

Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2015 March 15, 22:26