
- Scala - Home
- Scala - Overview
- Scala - Features
- Scala - Environment Setup
- Scala - Build Tool (SBT)
- Scala - REPL
- Scala - Dot & Dotty
- Scala - Basic Syntax
- Scala - Hello World Program
- Scala - Identifiers
- Scala - Keywords
- Scala - Comments
- Scala - Code Blocks
- Scala - Semicolon
- Scala - Constructs
- Scala - Expressions
- Scala - Input and Output
- Scala - Optional Braces
- Scala - Underscore (_)
- Data Types and Variables
- Scala - Data Types
- Scala - Type Bounds
- Scala - Context Bound
- Scala - Variances
- Scala - Type Hierarchy
- Scala - Variables
- Scala - Variable Scopes
- Scala - Literals
- Scala - Numeric Types
- Scala - Boolean Types
- Scala - Char Type
- Scala - Unit Types
- Scala - Strings
- Scala - Arrays
- Scala - Null Type
- Scala - Nothing
- Scala - Any Type
- Scala - AnyRef Type
- Scala - Unified Types
- Scala - Dates and Times
- Scala - Ranges
- Scala - Multidimensional Arrays
- Scala - WrappedArray
- Scala - StringBuilder
- Scala - String Interpolation
- Scala - StringContext
- Scala - Type Casting
- Scala var vs val
- Scala Operators
- Scala - Operators
- Scala - Rules for Operators
- Scala - Arithmetic Operators
- Scala - Relational Operators
- Scala - Logical Operators
- Scala - Bitwise Operators
- Scala - Assignment Operators
- Scala - Operators Precedence
- Scala - Symbolic Operators
- Scala - Range Operator
- Scala - String Concatenation Operator
- Scala Conditional Statements
- Scala - IF ELSE
- Scala - IF-ELSE-IF-ELSE Statement
- Scala - Nested IF-ELSE Statement
- Scala Loop Statements
- Scala - Loop Statements
- Scala - while Loop
- Scala - do-while Loop
- Scala - Nested Loops
- Scala - for Loop
- Scala - break Statement
- Scala - yield Keyword
- Scala Classes & Objects
- Scala - Classes & Objects
- Scala - Constructors
- Scala - Auxiliary Constructor
- Scala - Primary Constructor
- Scala - This Keyword
- Scala - Nested Classes
- Scala - Getters and Setters
- Scala - Object Private Fields
- Scala - Singleton Object
- Scala - Companion Objects
- Scala - Creating Executable Programs
- Scala - Stateful Object
- Scala - Enumerations
- Scala - Polymorphism
- Scala - Access Modifiers
- Scala - Apply Method
- Scala - Update Methods
- Scala - UnapplySeq Method
- Scala - Inheritance
- Scala - Extending a Class
- Scala - Method Overloading
- Scala - Method Overriding
- Scala - Generic Classes
- Scala - Generic Functions
- Scala - Superclass Construction
- Scala Methods & Functions
- Scala - Functions
- Scala - Main Methods
- Scala - Functions Call-by-Name
- Scala - Functions with Named Arguments
- Scala - Function with Variable Arguments
- Scala - Recursion Functions
- Scala - Default Parameter Values
- Scala - Functions without Parameters
- Scala - Implicit Parameters
- Scala - Higher-Order Functions
- Scala - Nested Functions
- Scala - Extension Methods
- Scala - Anonymous Functions
- Partially Applied Functions
- Scala - Lazy Val
- Scala - Pure Function
- Scala - Currying Functions
- Scala - Control Abstractions
- Scala - Corecursion
- Scala - Unfold
- Scala - Tail Recursion
- Scala - Infinite Sequences
- Scala - Dynamic Invocation
- Scala - Lambda Expressions
- Scala Collections
- Scala - Collections
- Mutable and Immutable Collections
- Scala - Lists
- Scala - Sets
- Scala - Maps
- Scala - TreeMap
- Scala - SortedMap
- Scala - Tuples
- Scala - Iterators
- Scala - Options
- Scala - Infinite Streams
- Scala - Parallel Collections
- Scala - Algebraic Data Types
- Scala Pattern Matching
- Scala - Pattern Matching
- Scala - Type Patterns
- Scala - Exception Handling
- Scala - Extractors
- Scala - Regular Expressions
- Scala Files I/O
- Scala - Files I/O
- Scala Advanced Concepts
- Scala - Closures
- Scala - Futures
- Scala - Promises
- Scala - Traits
- Scala - Trait Mixins
- Scala - Layered Traits
- Scala - Trait Linearization
- Scala - Sealed Traits
- Scala - Transparent Traits
- Scala - Literal Type Arithmetic
- Scala - Inline keyword
- Scala - Def, Var & Val
- Scala - Dropped Features
- Scala - BDD Testing
Scala - Hello World Program
Scala programming language supports JavaScript as a compilation target. Scala programs can run in Nodejs and in the web browser.
First Scala Program - Hello World
You should be comfortable writing a Hello, World program using the programming language that you use. This is the first and basic program in the Scala programming language.
This program can be divided into different parts.
Hello World Program Code
// Scala program to print Hello World! object HelloWorld { // Main Method def main(args: Array[String]){ // prints Hello World println("Hello World!") } }
Explanation of HelloWorld Program
Note that this program mainly has three important parts, these are explained as follows below.
-
object HelloWorld − This is the keyword which creates the objects. HelloWorld is the name of the object here. Objects are the instance of a class.
-
def main(args: Array[String]) − `def` is a keyword which defines a function in Scala. `main` and `Array[String]` are the name of the method and for command line arguments respectively.
- println("Hello World!") − This is a method which displays output on the console.
Running a Scala Hello World Program
You need to save this file HelloWorld.class format and save it. You can compile this program using the `scalac` command which will generate a few class files in the current directory. There will be fine named HelloWorld.class
Use the following command to compile and execute the above Scala program −
\> scalac HelloWorld.scala \> scala HelloWorld
The output will be −
Hello, World!
Ways to Compile and Execute a Scala Hello World Program
We can execute a Scala program in two modes: one is interactive mode and another is script mode.
1. Interactive Mode
Open the command prompt and use the following command to open Scala.
\>scala
If Scala is installed in your system, the following output will be displayed −
Welcome to Scala version 2.9.0.1 Type in expressions to have them evaluated. Type :help for more information.
Type the following text to the right of the Scala prompt and press the Enter key −
scala> println("Hello, Scala!");
It will produce the following result −
Hello, Scala!
2. Script Mode
Use the following instructions to write a Scala program in script mode. Open notepad and add the following code into it.
Example
object HelloWorld { /* This is my first java program. * This will print 'Hello World' as the output */ def main(args: Array[String]) { println("Hello, world!") // prints Hello World } }
Save the file as HelloWorld.scala.
Open the command prompt window and go to the directory where the program file is saved. The ‘scalac’ command is used to compile the Scala program and it will generate a few class files in the current directory. One of them will be called HelloWorld.class. This is a bytecode which will run on Java Virtual Machine (JVM) using ‘scala’ command.
Use the following command to compile and execute your Scala program.
\> scalac HelloWorld.scala \> scala HelloWorld
The output will be −
Hello, World!
Ways to print "Hello, World!" in Scala 2 and Scala 3
Scala 2
1. Using `println` method −
println("Hello, World!")
2. Using `console.println` method −
Console.println("Hello, World!")
3. Using printf method −
printf("Hello, World!\n")
4. Using a simple function −
def printHelloWorld(): Unit = { println("Hello, World!") } printHelloWorld()
Scala 3
There are syntax improvements in Scala 3 but you can still use Scala 2.
1. Using println method (same as Scala 2) −
println("Hello, World!")
2. Using a simple function (same as Scala 2) −
def printHelloWorld(): Unit = { println("Hello, World!") } printHelloWorld()
3. Using given and using for custom output −
given output: String = "Hello, World!" println(using output)
Asking for User Input
You can also ask for user Input for which you need to read input. There are various ways to read input from a command-line line. There is a simple way to use it with the help of `readLine` from `scala.io.StdIn`. You need to first import it.
Example program in Scala 2 −
import scala.io.StdIn.readLine object temperatureConversion { def main(args: Array[String]) = { println("Enter temperature in Celsius:") val celsius = readLine().toDouble val fahrenheit = celsius * 9/5 + 32 println("Temperature in Fahrenheit: " + fahrenheit) } }
Enter Celsius temperature, and it converts it to Fahrenheit.
We can have above program in Scala 3, like this −
import scala.io.StdIn @main def temperatureConversion: Unit = println("Enter temperature in Celsius:") val celsius = StdIn.readLine().toDouble val fahrenheit = celsius * 9/5 + 32 println(s"Temperature in Fahrenheit: $fahrenheit")