Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL

Step 1. Learn to use the Scala interpret... > Step 1. Learn to use the Scala inter... - Pg. 24

C HAPTER 2 · First Steps in Scala the resulting value. The interactive shell for Scala is simply called scala . You use it by typing scala at a command prompt: 2 $ scala Welcome to Scala version 2.8.1. Type in expressions to have them evaluated. Type :help for more information. scala> After you type an expression, such as 1 + 2 , and hit enter: scala> 1 + 2 The interpreter will print: res0: Int = 3 This line includes: · an automatically generated or user-defined name to refer to the com- puted value ( res0 , which means result 0), · a colon ( : ), followed by the type of the expression ( Int ), · an equals sign ( = ), · the value resulting from evaluating the expression ( 3 ). The type Int names the class Int in the package scala . Packages in Scala are similar to packages in Java: they partition the global namespace and provide a mechanism for information hiding. 3 Values of class Int corre- spond to Java's int values. More generally, all of Java's primitive types have corresponding classes in the scala package. For example, scala.Boolean corresponds to Java's boolean . scala.Float corresponds to Java's float . And when you compile your Scala code to Java bytecodes, the Scala com- piler will use Java's primitive types where possible to give you the perfor- mance benefits of the primitive types. you're using Windows, you'll need to type the scala command into the "Command Prompt" DOS box. 3 If you're not familiar with Java packages, you can think of them as providing a full name for classes. Because Int is a member of package scala , " Int " is the class's simple name, and " scala.Int " is its full name. The details of packages are explained in Chapter 13. 2 If 24