Outdated Notice
Since the release of the book, C Programming Language, most programming books have begun with a simple “Hello, world” example, and in keeping with tradition, here’s the source code for a Scala “Hello, world” example:
object Hello {
def main(args: Array[String]) = {
println("Hello, world")
}
}
Using a text editor, save that source code in a file named Hello.scala. After saving it, run this scalac
command at your command line prompt to compile it:
$ scalac Hello.scala
scalac
is just like javac
, and that command creates two new files:
- Hello$.class
- Hello.class
These are the same types of “.class” bytecode files you create with javac
, and they’re ready to work with the JVM.
Now you can run the Hello
application with the scala
command:
$ scala Hello
Discussion
Here’s the original source code again:
object Hello {
def main(args: Array[String]) = {
println("Hello, world")
}
}
Here’s a short description of that code:
- It defines a method named
main
inside a Scalaobject
namedHello
- An
object
is similar to aclass
, but you specifically use it when you want a single instance of that class- If you’re coming to Scala from Java, this means that
main
is just like astatic
method (We write more on this later)
- If you’re coming to Scala from Java, this means that
main
takes an input parameter namedargs
that is a string arrayArray
is a class that wraps the Javaarray
primitive
That Scala code is pretty much the same as this Java code:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world");
}
}
Going deeper: Scala creates .class files
As we mentioned, when you run the scalac
command it creates .class JVM bytecode files. You can see this for yourself. As an example, run this javap
command on the Hello.class file:
$ javap Hello.class
Compiled from "Hello.scala"
public final class Hello {
public static void main(java.lang.String[]);
}
As that output shows, the javap
command reads that .class file just as if it was created from Java source code. Scala code runs on the JVM and can use existing Java libraries — and both are terrific benefits for Scala programmers.
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