Writing functionsIntroduction to Scalaval variables and var variablesSome basic data types

Some basic data types

The most important basic types you will need first are:

Int
An integer in the range \(-2^{31}\) to \(2^{31}-1\).
Long
Sometimes we want to use integers larger than the maximal number allowed by the Int type. In that case, we can use the type Long, which covers the range \(2^{-63}\) to \(2^{63}-1\). To create a Long object, write a number followed by L, like 123L. (If you need even larger integers, you can use the type BigInt, which represents arbitrarily large integers. But BigInt is much slower.)
Boolean
Either true or false (be careful: different from Python, in Scala these are written with small letters).
Double
A floating-point representation of a real number.
Char
A single character. To create a Char object, write a character enclosed in single quotation marks, like 'A'. Scala supports Unicode characters, so a Char object can also be a Hangul syllable.
String
A sequence of Char. To create a String object, write a string enclosed in double quotation marks. Scala also supports the triple-quote syntax from Python to create long strings possibly containing new-line characters.

You will sometimes see that Scala indicates the type of a string as java.lang.String. Since Scala runs using the Java virtual machine and can use Java libraries, Scala uses Java strings for compatibility. We can just write String to indicate the string type.

Special characters can be placed in Char and String literals using a backslash. For instance, \n is a new-line character, \t a tabulator, \' and \" are single and double quotes, and \\ is a backslash character. (Inside triple-quote strings, the backslash is not interpreted like this!)

Here is a list of important String-methods.

For completeness, the other basic types in Scala are Byte, Short, and Float. Don't use them unless you know what you are doing.

Converting between types

You can convert objects between the basic types by using their conversion methods:

scala> 98.toChar
res1: Char = b
scala> 'c'.toDouble
res2: Double = 99.0
scala> 'c'.toString
res3: java.lang.String = c
scala> 98.55.toInt
res4: Int = 98

Operators

Scala's numerical operators are +, -, /, and %. Like in Python 2, Java, and C, the division operators / and % perform an integer division if both operands are integer objects. Note that there is no power operator in Scala (you can use the math.pow library function to do exponentiation).

You can use the shortcuts +=, -=, etc.

You can compare numbers or strings using ==, !=, <, >, <=, and >=.

The boolean operators are ! for not, && for and, and || for or, as in C and Java. (These somewhat strange operators were invented for C in the early 1970s, and have somehow survived into modern programming languages.) Like in Python, Java, and C, these operators only evaluate their right hand operand when the result is not already known from the value of the left hand operand.

Sooner or later, you may meet the bitwise operators: &, |, ^, ~, <<, >>, and >>>. They work on the bit-representation of integers. (A common mistake is to try to use ^ for exponentiation—it does something completely different, namely a logical exclusive-or.) Don't use these operators unless you know what you are doing.

The equality and inequality operators == and != can be used for objects of any type, even to compare objects of different type.

One of the great features of Scala is that you can define your own operators in a very flexible way. It is easy to abuse this feature, though, and we will not use it until later in the course.

In Scala, the mathematical constants and functions are methods of the package math, the equivalent of the Python math module. For instance:

scala> val pi = math.Pi
pi: Double = 3.141592653589793
scala> val t = math.sin(pi/6)          
t: Double = 0.49999999999999994
scala> math.sin(pi/4)
res1: Double = 0.7071067811865475
scala> math.sqrt(2.0)/2
res2: Double = 0.7071067811865476
If you don't like the long function names, you can import some of the functions:
scala> import math.sin
import math.sin
scala> sin(math.Pi/4)
res1: Double = 0.7071067811865475
Here, we imported only the sin-function. We could also import several functions at once:
scala> import math.{Pi,sin,cos,sqrt}
import math.{Pi, sin, cos, sqrt}
scala> sin(Pi/4)
res1: Double = 0.7071067811865475
scala> sqrt(2.0)/2
res2: Double = 0.7071067811865476
You can even import everything in the math-package using this syntax:
scala> import math._
But note that unlike in Python, it is not necessary to write an import-statement to use the math-package!
Writing functionsIntroduction to Scalaval variables and var variablesSome basic data types