<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

    主站蜘蛛池模板: 亚洲精品无码人妻无码| 丰满妇女做a级毛片免费观看| 亚洲二区在线视频| 久久国产色AV免费观看| 亚洲成Av人片乱码色午夜| 永久免费av无码网站yy| 亚洲国产精华液网站w| 久久精品国产大片免费观看| 亚洲成A人片在线观看无码不卡 | 国产1000部成人免费视频| 亚洲国产电影在线观看| 成人免费毛片视频| 亚洲av永久无码精品秋霞电影秋 | 国内精品99亚洲免费高清| 99re6在线精品免费观看| 国产成人A在线观看视频免费| 亚洲天堂2017无码中文| 九九久久国产精品免费热6| 亚洲精品网站在线观看不卡无广告 | 午夜亚洲国产理论秋霞| 99免费观看视频| 亚洲不卡1卡2卡三卡2021麻豆| 久久久久国色AV免费看图片| 日韩亚洲翔田千里在线| 自拍偷自拍亚洲精品被多人伦好爽 | 日韩免费观看视频| 亚洲国产精品综合久久网各| 欧美好看的免费电影在线观看| 亚洲国产综合AV在线观看| 亚洲国产精品成人网址天堂| 在线看片免费人成视频福利| 亚洲国产精品专区| 免费在线观看黄网站| 成人久久免费网站| 香蕉大伊亚洲人在线观看| 国产国拍亚洲精品福利 | 最近免费中文字幕大全高清大全1| 亚洲福利一区二区精品秒拍| 国产一区二区三区免费在线观看| 暖暖在线视频免费视频| 中文有码亚洲制服av片|