Code Examples for

Programming in Scala, Fifth Edition

Return to chapter index

5 Basic Types and Operations

Sample run of chapter's interpreter examples

5.1 Some basic types

5.2 Literals


val hex = 0x5 // 5: Int val hex2 = 0x00FF // 255: Int val magic = 0xcafebabe // -889275714: Int val billion = 1_000_000_000 // 1000000000: Int
val dec1 = 31 // 31: Int val dec2 = 255 // 255: Int val dec3 = 20 // 20: Int
val prog = 0XCAFEBABEL // 3405691582: Long val tower = 35L // 35: Long val of = 31l // 31: Long
val little: Short = 367 // 367: Short val littler: Byte = 38 // 38: Byte
val big = 1.2345 // 1.2345: Double val bigger = 1.2345e1 // 12.345: Double val biggerStill = 123E45 // 1.23E47: Double val trillion = 1_000_000_000e3 // 1.0E12: Double
val little = 1.2345F // 1.2345: Float val littleBigger = 3e5f // 300000.0: Float
val anotherDouble = 3e5 // 300000.0: Double val yetAnother = 3e5D // 300000.0: Double
import scala.language.experimental.genericNumberLiterals
val invoice: BigInt = 1_000_000_000_000_000_000_000 val pi: BigDecimal = 3.1415926535897932384626433833
scala> val a = 'A' val a: Char = A
scala> val d = '\u0041' val d: Char = A scala> val f = '\u0044' val f: Char = D
scala> val B\u0041\u0044 = 1 val BAD: Int = 1
scala> val backslash = '\\' val backslash: Char = \
scala> val hello = "hello" val hello: String = hello
scala> val escapes = "\\\"\'" val escapes: String = \"'
// In file basic-types-and-operations/help1.scala println("""Welcome to Ultamix 3000. Type "HELP" for help.""")
Welcome to Ultamix 3000. Type "HELP" for help.
// In file basic-types-and-operations/help2.scala println("""|Welcome to Ultamix 3000. |Type "HELP" for help.""".stripMargin)
Welcome to Ultamix 3000. Type "HELP" for help.
val bool = true // true: Boolean val fool = false // false: Boolean

5.3 String interpolation


val name = "reader" println(s"Hello, $name!")
scala> s"The answer is ${6 * 7}." val res0: String = The answer is 42.
println(raw"No\\\\escape!") // prints: No\z\z\z{\z}escape!
scala> f"${math.Pi}%.5f" val res1: String = 3.14159
scala> val pi = "Pi" val pi: String = Pi scala> f"pi is approximately {math.Pi}%.8f." val res2: String = Pi is approximately 3.14159265.

5.4 Operators are methods


val sum = 1 + 2 // Scala invokes 1.+(2)
scala> val sumMore = 1.+(2) val sumMore: Int = 3
scala> val longSum = 1 + 2L // Scala invokes 1.+(2L) val longSum: Long = 3
scala> val s = "Hello, world!" val s: String = Hello, world! scala> s indexOf 'o' // Scala invokes s.indexOf('o') val res0: Int = 4
scala> -2.0 // Scala invokes (2.0).unary_- val res2: Double = -2.0 scala> (2.0).unary_- val res3: Double = -2.0
scala> val s = "Hello, world!" val s: String = Hello, world! scala> s.toLowerCase val res4: String = hello, world!
scala> import scala.language.postfixOps scala> s toLowerCase val res5: String = hello, world!

5.5 Arithmetic operations


1.2 + 2.3 // 3.5: Double 3 - 1 // 2: Int 'b' - 'a' // 1: Int 2L * 3L // 6: Long 11 / 4 // 2: Int 11 % 4 // 3: Int 11.0f / 4.0f // 2.75: Float 11.0 % 4.0 // 3.0: Double
math.IEEEremainder(11.0, 4.0) // -1.0: Double
val neg = 1 + -3 // -2: Neg val y = +3 // 3: Int -neg // 2: Int

5.6 Relational and logical operations


1 > 2 // false: Boolean 1 < 2 // true: Boolean 1.0 <= 1.0 // true: Boolean 3.5f >= 3.6f // false: Boolean 'a' >= 'A' // true: Boolean val untrue = !true // false: Boolean
val toBe = true // true: Boolean val question = toBe || !toBe // true: Boolean val paradox = toBe && !toBe // false: Boolean
scala> def salt() = { println("salt"); false } def salt(): Boolean scala> def pepper() = { println("pepper"); true } def pepper(): Boolean scala> pepper() && salt() pepper salt val res21: Boolean = false scala> salt() && pepper() salt val res22: Boolean = false
scala> salt() & pepper() salt pepper val res23: Boolean = false

5.7 Bitwise operations


1 & 2 // 0: Int 1 | 2 // 3: Int 1 ^ 3 // 2: Int ~1 // -2: Int
-1 >> 31 // -1: Int -1 >>> 31 // 1: Int 1 << 2 // 4: Int

5.8 Object equality


1 == 2 // false: Boolean 1 != 2 // true: Boolean 2 == 2 // true: Boolean
List(1, 2, 3) == List(1, 2, 3) // true: Boolean List(1, 2, 3) == List(4, 5, 6) // false: Boolean
1 == 1.0 // true: Boolean List(1, 2, 3) == "hello" // false: Boolean
List(1, 2, 3) == null // false: Boolean null == List(1, 2, 3) // false: Boolean
("he" + "llo") == "hello" // true: Boolean

5.9 Operator precedence and associativity


(2 + 2) * 7
2 << 2 + 2 // 32: Int
2 + 2 << 2 // 16: Int
x *= y + 1
x *= (y + 1)
{ val x = a; b.:::(x) }

5.10 Rich operations

5.11 Conclusion

For more information about Programming in Scala, Fifth Edition (the "Stairway Book"), please visit:

http://www.artima.com/shop/programming_in_scala_5ed

and:

http://booksites.artima.com/programming_in_scala_5ed

Copyright © 2007-2020 Artima, Inc. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.