<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 閱讀(232) 評論(0)  編輯  收藏 所屬分類: Scala / Java

    主站蜘蛛池模板: 亚洲国产精品综合福利专区| 国产亚洲成av人片在线观看| 特级精品毛片免费观看| 中文字幕亚洲专区| 国产精品一区二区三区免费| 国产中文字幕免费| 牛牛在线精品观看免费正| 免费va人成视频网站全| 免费国产a理论片| 亚洲日韩在线第一页| 一个人看的www免费高清| 亚洲视频在线一区二区| 精品国产免费人成网站| 亚洲成a人片在线观看日本| 91精品国产免费久久国语蜜臀| 亚洲欧洲视频在线观看| 中文字幕人成无码免费视频| 亚洲日韩一区精品射精| 国产亚洲精品免费| 中文字幕成人免费高清在线视频 | 亚洲人成色777777精品| 精品少妇人妻AV免费久久洗澡| 国产AV日韩A∨亚洲AV电影 | 亚洲高清无码专区视频| 中国毛片免费观看| 亚洲精品mv在线观看| 精品久久洲久久久久护士免费| 黄色一级视频免费观看| 亚洲国产成人片在线观看无码 | 免费一区二区视频| 免费人成在线观看视频高潮| 亚洲成人免费在线观看| 国产一级特黄高清免费大片| 免费观看男人吊女人视频| 国产成人A亚洲精V品无码| 99久在线国内在线播放免费观看 | 日韩精品亚洲专区在线影视 | 亚洲精品成人网站在线播放| 在线观看免费亚洲| 久久精品国产大片免费观看| 亚洲性无码一区二区三区|