Home > Projects > Hello World



Scala


Scala Overview




Simple Hello World


There are a number of ways to use Scala. The easiest is with an IDE (Integrated Development Environment).

For the examples on this page, the Scala IDE build of Eclipse SDK was used.


Unlike the stand-alone Hello World application for Java, there is no class declaration and nothing is declared to be static.

 object HelloWorld extends App {
   println("Hello, World!")
 }

Scala (Two-dimensional Array)


There are better and simpler ways to print Ascii art than this two-dimensional array example. In the past I worked with junior developers who didn't understand arrays. Basically here is the type of example I showed them to illustrate the concept.


Periods were used as placemarkers for the spaces to make it visually easier.



Use the Array.ofDim method to create an array, named "hello" with five rows and eleven columns:

object Hello extends App {
  var rows = 5
  var cols = 11
  var hello = Array.ofDim[String](rows, cols)

For the populating the array, refer to the illustration above to add elements to it:

   //row 1
    hello(0)(0) = " _    "
    hello(0)(1) = "     "
    hello(0)(2) = " _ "
    hello(0)(3) = " _ "
    hello(0)(4) = "      "
    hello(0)(5) = "         "
    hello(0)(6) = "     "
    hello(0)(7) = "     "
    hello(0)(8) = " _ "
    hello(0)(9) = "    _ "
    hello(0)(10) = " _ "
   
  //row 2
    hello(1)(0) = "| |__ "
    hello(1)(1) = " ___ "
    hello(1)(2) = "| |"
    hello(1)(3) = "| |"
    hello(1)(4) = " ___  "
    hello(1)(5) = " ___  ___"
    hello(1)(6) = " ___ "
    hello(1)(7) = " ___ "
    hello(1)(8) = "| |"
    hello(1)(9) = " __| |"
    hello(1)(10) = "| |"

Note the use of triple double quotes on some of the strings where escape characters needed to be used as actual characters in the Ascii art.

(e.g. val str = """ \""" )

  
   //row 3
    hello(2)(0) = """|    \"""
    hello(2)(1) = """/ __\"""
    hello(2)(2) = "| |"
    hello(2)(3) = "| |"
    hello(2)(4) = """/ _ \ """
    hello(2)(5) = """ \  ||  /"""
    hello(2)(6) = """/ _ \"""
    hello(2)(7) = """|  _\"""
    hello(2)(8) = "| |"
    hello(2)(9) = "/ _' |"
    hello(2)(10) = "| |"
    
  //row 4   
    hello(3)(0) = "|_||_|"
    hello(3)(1) = """\___|"""
    hello(3)(2) = "|_|"
    hello(3)(3) = "|_|"
    hello(3)(4) = """\___/ """
    hello(3)(5) = """_ \_/\_/ """ 
    hello(3)(6) = """\___/"""
    hello(3)(7) = "|_|  "
    hello(3)(8) = "|_|"
    hello(3)(9) = """\____|"""
    hello(3)(10) = "|_|" 
      
   //row 5
    hello(4)(0) = "      "
    hello(4)(1) = "     "
    hello(4)(2) = "   "
    hello(4)(3) = "   "
    hello(4)(4) = "      |/"
    hello(4)(5) = "       " 
    hello(4)(6) = "     "
    hello(4)(7) = "     "
    hello(4)(8) = "   "
    hello(4)(9) = "      "
    hello(4)(10) = "(_)" 
 

Next, iterate over the array with for loops:

  
    for ( i <- 0 until rows) {
        for( j <- 0 until cols) {   
        print( " " + hello(i)(j))		
  	  }
	    println()
   }
}

Below is the output:


 _          _  _                           _     _  _ 

| |__  ___ | || | ___   ___  ___ ___  ___ | | __| || |

|    \/ __\| || |/ _ \  \  ||  // _ \|  _\| |/ _' || |

|_||_|\___||_||_|\___/ _ \_/\_/ \___/|_|  |_|\____||_|

                      |/                           (_)



Scala (Multi-dimensional Array)


To create an array with more dimensions, just follow the same pattern.

The following example is from the Scala Cookbook, by Alvin Alexander

Code for a three-dimensional array:

val x, y, z = 10
val a = Array.ofDim[Int](x,y,z)
for {
  i <- 0 until x
  j <- 0 until y
  k <- 0 until z
} println(s"($i)($j)($k) = ${a(i)(j)(k)}")

For more information on the differences between Scala and Java refer to this link in Wikipedia