Sample run of chapter's interpreter examples
scala> val hex = 0x5 hex: Int = 5 scala> val hex2 = 0x00FF hex2: Int = 255 scala> val magic = 0xcafebabe magic: Int = -889275714
scala> val oct = 035 // (35 octal is 29 decimal) oct: Int = 29 scala> val nov = 0777 nov: Int = 511 scala> val dec = 0321 dec: Int = 209
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 c = '\101' c: 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: java.lang.String = hello
scala> val escapes = "\\\"\'" escapes: java.lang.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> s.name res20: String = aSymbol
scala> val bool = true bool: Boolean = true scala> val fool = false fool: Boolean = false
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: java.lang.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: java.lang.String = Hello, world! scala> s.toLowerCase res4: java.lang.String = hello, world!
scala> s toLowerCase res5: java.lang.String = hello, world!
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
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 thisIsBoring = !true thisIsBoring: Boolean = false scala> !thisIsBoring res21: Boolean = true
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 res22: Boolean = false scala> salt() && pepper() salt res23: Boolean = false
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
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
(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) }
For more information about Programming in Scala (the "Stairway Book"), please visit: http://www.artima.com/shop/programming_in_scala and: |
Copyright © 2007-2008 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. |