Some basic data types |
The most important basic types you will need first are:
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.
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
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.7071067811865476If 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.7071067811865475Here, 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.7071067811865476You 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!
Some basic data types |