Movatterモバイル変換


[0]ホーム

URL:


This page is no longer maintained — Please continue to the home page atwww.scala-lang.org

Home

Scala Main Menu

Home »Documentation »Setup & Getting Started » Getting Started with Scala

Getting Started with Scala

Created by admin on 2008-07-15. Updated: 2008-11-07, 12:03

The "Hello,world!" Program

As a first example, we use the standardHello 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 methodmain 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 !

Thescala 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 HelloWorldscala> 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 thescala command).

Thebash shell scriptscript.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 filescript.sh has execute access and the search path for thescala command is specified in thePATH environment variable.

Compile it !

Thescalac command compiles one (or more) Scala source file(s) and generates Java bytecode which can be executed on anystandard JVM; the Scala compiler works similarly tojavac, the Java compiler of theJava SDK.

> scalac HelloWorld.scala

By defaultscalac 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 !

Thescala 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 thescala command has to be a top-level object. If that object is followed by the clauseextends Application, then all statements contained in that object will be executed; otherwise you have to add a methodmain which will act as the entry point of your program.

Here is how it looks like:

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

 

Scala Quick Links

Featured News

User login


will be sent securely

Create new account

Retrieve lost password

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland


[8]ページ先頭

©2009-2025 Movatter.jp