<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Chan Chen Coding...

    Scala First Taste

    The "Hello, world!" Program

    As a first example, we use the standard Hello world program to demonstrate the use of the Scala tools without knowing too much about the language.

      object HelloWorld {     
          def main(args: Array[String]) {
             println("Hello, world!")
    }
    }

    The structure of this program should be familiar to Java programmers: it consists of the method main which prints out a friendly greeting to the standard output.

    We assume that both the Scala software and the user environment are set up correctly. For example:

    EnvironmentVariableValue (example)
    Unix
    $SCALA_HOME $PATH
    /usr/local/share/scala $PATH:$SCALA_HOME/bin 
    Windows
    %SCALA_HOME% %PATH%
    c:\Progra~1\Scala %PATH%;%SCALA_HOME%\bin

    Run it interactively !

    The scala command starts an interactive shell where Scala expressions are interpreted interactively.

    > scala   
    This is a Scala shell.
    Type in expressions to have them evaluated.
    Type :help for more information.
    scala> object HelloWorld {
    | def main(args: Array[String]) {
    | println("Hello, world!")
    | }
    | } defined module HelloWorld
    scala> HelloWorld.main(null)
    Hello, world!
    unnamed0: Unit = ()
    scala>:q

    The shortcut :q stands for the internal shell command :quit used to exit the interpreter.

    Script it !

    The above Scala program may also be run as a shell script respectively as a batch command (see the examples in the man pages of the scala command).

    The bash shell script script.sh containing the following Scala code (and shell preamble)

    #!/bin/sh
    exec scala "$0" "$@"
    !#
    object HelloWorld {
        def main(args: Array[String]) {
        println("Hello, world! " + args.toList)
        }
    }
    HelloWorld.main(args)

    can be run directly from the command shell:

      > ./script.sh

    Note: We assume here the file script.sh has execute access and the search path for the scala command is specified in the PATH environment variable.

    Compile it !

    The scalac command compiles one (or more) Scala source file(s) and generates Java bytecode which can be executed on any standard JVM; the Scala compiler works similarly to javac, the Java compiler of the Java SDK.

      > scalac HelloWorld.scala

    By default scalac generates the class files into the current working directory. You may specify a different output directory using the -d option.

      > scalac -d classes HelloWorld.scala

    Execute it !

    The scala command executes the generated bytecode with the appropriate options:

      > scala HelloWorld

    scala allows us to specify command options, such as the -classpath (or -cp) option:

      > scala -classpath classes HelloWorld

    The argument of the scala command has to be a top-level object. If that object is followed by the clause extendsApplication, then all statements contained in that object will be executed; otherwise you have to add a method main which will act as the entry point of your program.

    Here is how it looks like:

    object HelloWorld2 extends Application {   
        println("Hello, world!")
    }


    -----------------------------------------------------
    Silence, the way to avoid many problems;
    Smile, the way to solve many problems;

    posted on 2012-02-25 21:50 Chan Chen 閱讀(238) 評(píng)論(0)  編輯  收藏 所屬分類: Scala / Java

    主站蜘蛛池模板: 女人体1963午夜免费视频| 免费阿v网站在线观看g| 四虎影永久在线高清免费| 亚洲老熟女五十路老熟女bbw| 在线v片免费观看视频| 无码国产亚洲日韩国精品视频一区二区三区 | 久久亚洲色WWW成人欧美| 四虎影院在线免费播放| 亚洲日韩一区二区三区| 日韩免费一级毛片| 天堂亚洲免费视频| 亚洲精品国产品国语在线| 久久美女网站免费| 一本色道久久88亚洲综合| 无人视频在线观看免费播放影院 | 美女18一级毛片免费看| 在线人成免费视频69国产| 久久99国产亚洲精品观看| 麻豆视频免费播放| 亚洲熟妇无码八AV在线播放| 亚洲中文字幕一区精品自拍| 无码国产精品一区二区免费模式| 亚洲短视频男人的影院| 国产1024精品视频专区免费| 亚洲国产成人无码AV在线影院| 免费在线黄色网址| 无码人妻AV免费一区二区三区| 亚洲AV无码一区二区三区牛牛| 免费又黄又爽的视频| 野花香高清在线观看视频播放免费| 亚洲邪恶天堂影院在线观看| 人禽伦免费交视频播放| 亚洲欧洲免费视频| 在线观看免费精品国产| 免费精品久久天干天干| 亚洲1234区乱码| 青春禁区视频在线观看直播免费| 风间由美在线亚洲一区| 亚洲综合精品一二三区在线| 免费无码黄动漫在线观看| 亚洲va久久久久|