Outdated Notice
While that first “Hello, World” example works just fine, Scala provides a way to write applications more conveniently. Rather than including a main
method, your object
can just extend the App
trait, like this:
object Hello2 extends App {
println("Hello, world")
}
If you save that code to Hello.scala, compile it with scalac
and run it with scala
, you’ll see the same result as the previous lesson.
What happens here is that the App
trait has its own main
method, so you don’t need to write one. We’ll show later on how you can access command-line arguments with this approach, but the short story is that it’s easy: they’re made available to you in a string array named args
.
We haven’t mentioned it yet, but a Scala
trait
is similar to an abstract class in Java. (More accurately, it’s a combination of an abstract class and an interface — more on this later!)
Extra credit
If you want to see how command-line arguments work when your object extends the App
trait, save this source code in a file named HelloYou.scala:
object HelloYou extends App {
if (args.size == 0)
println("Hello, you")
else
println("Hello, " + args(0))
}
Then compile it with scalac
:
scalac HelloYou.scala
Then run it with and without command-line arguments. Here’s an example:
$ scala HelloYou
Hello, you
$ scala HelloYou Al
Hello, Al
This shows:
- Command-line arguments are automatically made available to you in a variable named
args
. - You determine the number of elements in
args
withargs.size
(orargs.length
, if you prefer). args
is anArray
, and you accessArray
elements asargs(0)
,args(1)
, etc. Becauseargs
is an object, you access the array elements with parentheses (not[]
or any other special syntax).
Contributors to this page:
Contents
- Introduction
- Prelude꞉ A Taste of Scala
- Preliminaries
- Scala Features
- Hello, World
- Hello, World - Version 2
- The Scala REPL
- Two Types of Variables
- The Type is Optional
- A Few Built-In Types
- Two Notes About Strings
- Command-Line I/O
- Control Structures
- The if/then/else Construct
- for Loops
- for Expressions
- match Expressions
- try/catch/finally Expressions
- Scala Classes
- Auxiliary Class Constructors
- Supplying Default Values for Constructor Parameters
- A First Look at Scala Methods
- Enumerations (and a Complete Pizza Class)
- Scala Traits and Abstract Classes
- Using Scala Traits as Interfaces
- Using Scala Traits Like Abstract Classes
- Abstract Classes
- Scala Collections
- The ArrayBuffer Class
- The List Class
- The Vector Class
- The Map Class
- The Set Class
- Anonymous Functions
- Common Sequence Methods
- Common Map Methods
- A Few Miscellaneous Items
- Tuples
- An OOP Example
- sbt and ScalaTest
- The most used scala build tool (sbt)
- Using ScalaTest with sbt
- Writing BDD Style Tests with ScalaTest and sbt
- Functional Programming
- Pure Functions
- Passing Functions Around
- No Null Values
- Companion Objects
- Case Classes
- Case Objects
- Functional Error Handling in Scala
- Concurrency
- Scala Futures
- Where To Go Next