First steps in scala - part I: hello world

bettyes

BettyES

Posted on October 27, 2018

First steps in scala - part I: hello world

From the start

This part is for 'bloody' beginners that are eager to do their very first steps in Scala, but are not quite certain how to start. I will start with some simple "hello world" examples to introduce variables and methods. In the next part I will explore objects a bit more. I have been using IntelliJ IDE and am working on a Unix based system.

To set up IntelliJ for Scala, have a look here. Many of the concepts covered here can be found at tutorial point. Originally this how-to-article has been published on my private website

Basics

  • Scala is object oriented and functional
  • Every value is an object
  • Every function is a value
  • Every function is an object (confused? Functions are created with "val", methods are created with "def" and are part of a class/object. Don't worry too much about it for the moment. In case you do, have a read here)
  • No redundant type information required
  • The biggest difference between Java and Scala is that the “;” line end is optional
  • CamelCase naming convention (each new word starts in capital letters) function names start lower case: getValue Class names start with Capital letters: ValueClass

Some understanding of Java is definitely helpful to get started with Scala. Scala components used here in short

  • Objects - have states and behaviours, e.g. a car may have states: color, make, and behaviour: move
  • Classes - templates for creating objects
  • Methods - are basically behaviours. They can occur object independent as functions.
  • Fields - instance variables of an object

To run scala one can choose between interactive mode (in the terminal) vs. script mode (in your IDE).

To run interactive mode: (1) open your terminal, (2) install scala: brew install scala (If you need to install brew click here), (3) run scala: scala, (4) stop scala with ctrl+c

A classic: "Hello, world!"

Lets have a look at our first hello world example. Every application needs an entry point, the main method. In scala, if you create this method manually it will be within an object (For alternative ways to launch you project have a look here). In script-mode create a new file helloworld.scala which should contain an object with the main method. In there you place your print arguement. (use println() for ending in a new line, else print()).

Contents of helloworld.scala

object HelloWorld { 
def main(args: Array[String]) {
println("Hello, world!") }
}

You can run this file directly in IntellyJ's console, or in the terminal by cd ~/path/FolderContainingTheFile/ and typing scala helloworld.scala.

Contrary to Java you can drop the ';' at the end of a line. However, you can keep it as a line break in between statements. Assigning strings to variables, also does not nescessarily require a declaration of the data type. Variables themselves can be mutable (var) and immutable (val).

Lets run the "hello world" again, but this time including concepts mentioned above:

object HelloWorld {
def main(args: Array[String]) {
val s: String = ”Hello, world!"; println(s)
  }
}

and, finally, we can also move the print-statement into a function, that we call afterwards in our main method.

object HelloWorld {
  def main(args: Array[String]) {
    val name: String = "Joe"
    hello(name)
  }

  def hello(s: String ): Unit ={
    println(s"Hello, $s!")
  }
}

Please note that in above's print-statement, by adding 's' infront of " , we enable calling a variable (${variablereference}) within a String, called String interpolation.

In IntelliJ IDE it should look like this:

hello world in IntelliJ

Instead of typing scala helloWorld.scala in the terminal you can just run your helloWorld within IntelliJ by pressing the green 'play' butten next to your main method.

Data types

Finally, below some data types used in scala. Regarding numbers you will most of the time be dealing with int (whole numbers) and double (fractions):

  • Byte: 8 bit signed value from -128 to +127
  • Short: 16 bit signed value. Range -32768 to +32767
  • Int: 32 bit signed value. Range -2147483648 to +2147483647
  • Long: 64 bit signed value. -9 223 372 036 854 775 808 to +9 223 372 036 854 775 807
  • Float: 32 bit IEEE 754 single-precision float
  • Double: 64 bit IEEE 754 double-precision float
  • Char: 16 bit unsigned Unicode character. Range from U+0000 to U+FFFF
  • String: A sequence of Chars
  • Boolean: Either the literal true or the literal false

Next part:

In the next blog part some more object oriented programming (including pros and cons), creating a car class, grid class for simple visual output and exploring some standard input.

💖 💪 🙅 🚩
bettyes
BettyES

Posted on October 27, 2018

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related