Code Examples for

Programming in Scala, Third 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


scala> val hex = 0x5 hex: Int = 5 scala> val hex2 = 0x00FF hex2: Int = 255 scala> val magic = 0xcafebabe magic: Int = -889275714
scala> val dec1 = 31 dec1: Int = 31 scala> val dec2 = 255 dec2: Int = 255 scala> val dec3 = 20 dec3: Int = 20
scala> val prog = 0XCAFEBABEL prog: Long = 3405691582 scala> val tower = 35L tower: Long = 35 scala> val of = 31l of: Long = 31
scala> val little: Short = 367 little: Short = 367 scala> val littler: Byte = 38 littler: Byte = 38
scala> val big = 1.2345 big: Double = 1.2345 scala> val bigger = 1.2345e1 bigger: Double = 12.345 scala> val biggerStill = 123E45 biggerStill: Double = 1.23E47
scala> val little = 1.2345F little: Float = 1.2345 scala> val littleBigger = 3e5f littleBigger: Float = 300000.0
scala> val anotherDouble = 3e5 anotherDouble: Double = 300000.0 scala> val yetAnother = 3e5D yetAnother: Double = 300000.0
scala> val a = 'A' a: Char = A
scala> val d = '\u0041' d: Char = A scala> val f = '\u0044' f: Char = D
scala> val B\u0041\u0044 = 1 BAD: Int = 1
scala> val backslash = '\\' backslash: Char = \
scala> val hello = "hello" hello: String = hello
scala> val escapes = "\\\"\'" 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.
scala> def updateRecordByName(r: Symbol, value: Any) = { | // code goes here | } updateRecordByName: (Symbol,Any)Unit
scala> updateRecordByName(favoriteAlbum, "OK Computer") <console>:6: error: not found: value favoriteAlbum updateRecordByName(favoriteAlbum, "OK Computer") ^
scala> updateRecordByName('favoriteAlbum, "OK Computer")
scala> val s = 'aSymbol s: Symbol = 'aSymbol scala> val nm = s.name nm: String = aSymbol
scala> val bool = true bool: Boolean = true scala> val fool = false fool: Boolean = false

5.3 String interpolation


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

5.4 Operators are methods


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

5.5 Arithmetic operations


scala> 1.2 + 2.3 res6: Double = 3.5 scala> 3 - 1 res7: Int = 2 scala> 'b' - 'a' res8: Int = 1 scala> 2L * 3L res9: Long = 6 scala> 11 / 4 res10: Int = 2 scala> 11 % 4 res11: Int = 3 scala> 11.0f / 4.0f res12: Float = 2.75 scala> 11.0 % 4.0 res13: Double = 3.0
scala> math.IEEEremainder(11.0, 4.0) res14: Double = -1.0
scala> val neg = 1 + -3 neg: Int = -2 scala> val y = +3 y: Int = 3 scala> -neg res15: Int = 2

5.6 Relational and logical operations


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

5.7 Bitwise operations


scala> 1 & 2 res24: Int = 0 scala> 1 | 2 res25: Int = 3 scala> 1 ^ 3 res26: Int = 2 scala> ~1 res27: Int = -2
scala> -1 >> 31 res28: Int = -1 scala> -1 >>> 31 res29: Int = 1 scala> 1 << 2 res30: Int = 4

5.8 Object equality


scala> 1 == 2 res31: Boolean = false scala> 1 != 2 res32: Boolean = true scala> 2 == 2 res33: Boolean = true
scala> List(1, 2, 3) == List(1, 2, 3) res34: Boolean = true scala> List(1, 2, 3) == List(4, 5, 6) res35: Boolean = false
scala> 1 == 1.0 res36: Boolean = true scala> List(1, 2, 3) == "hello" res37: Boolean = false
scala> List(1, 2, 3) == null res38: Boolean = false scala> null == List(1, 2, 3) res39: Boolean = false
scala> ("he" + "llo") == "hello" res40: Boolean = true

5.9 Operator precedence and associativity


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

5.10 Rich wrappers

5.11 Conclusion

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

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

and:

http://booksites.artima.com/programming_in_scala_3ed

Copyright © 2007-2016 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.