Posts Trying Scala
Post
Cancel

Trying Scala

Scala

Download

Extract the tgz file using any archive utility.

Now go to the bin directory in extracted scala folder and type

1
scala (windows) or ./scala (linux/cygwin).

You will see something like below.

Where X is for scala version and Y for your java version.

1
2
3
4
5
6

Welcome to Scala version 2.10.X (YY_JAVA_YY).

Type in expressions to have them evaluated.

Type :help for more information.
1
scala>

Now type 2+3 and hit enter an you will see output like below

1
2
scala> 2+3
res0: Int = 5

Here res0 is a variable of type Int which has a value of 5.

Now to print the value of res0 variable type printf(“value of variable is “+res0);

You will see output like this

1
2
scala> printf("value of variable is "+res0);
value of variable is 5

Now let us create our own variable and assign value

1
2
3
var i = 10;
val j = 20; //notice it is val not var !
var total = i + j;

You will see output as below

1
2
scala> var i = 10;
i: Int = 10
1
2
scala> val j = 20;
j: Int = 20
1
2
scala> var total = i + j;
total: Int = 30

Now let us set new value into i and j

1
2
3
i = 50;

j = 100;

You will see output as below

1
2
scala> i = 50;
i: Int = 50
1
2
3
4
scala> j = 100;
<console>:8: error: reassignment to val
       j = 100;
         ^

As per above logic i was defined as “var” and j was defined as “val”.

Think of “val” and java “final” modifier which means value once defined to a variable of type “val” cannot be changed.

so

1
val j = 20; in scala

is same as

1
final int j=20; in java.

Let’s define a variable.

Your output will be

1
2
scala> var balance : Long = 10110011001L;
balance: Long = 10110011001

As per above syntax we are creating a variable of type “var” which means we can update the variable value.

variable name is balance.

variable type is Long

Your output will be

1
2
scala> val accountNo : Long = 123450011L;
accountNo: Long = 123450011

Logically user account number never changes, so we should set it to type val or final in java.

Let’s create some Strings now.

Type “hello, welcome to scala REPL !!”

Your output will be

1
2
3
scala> "hello, welcome to scala REPL !!"

res1: String = hello, welcome to scala REPL !!

The value has been stored in new variable called res1 of type String

Now to print the value of res1 variable type printf(res1);

You will see output like this

1
2
scala> printf(res1);
hello, welcome to scala REPL !!

Now let’s create our own String variable

1
2
3
scala> var langName : String = "Scala"

langName: String = Scala

So the above syntax says, we are creating a variable of type “var” which means we can change the variable value.

variable name is langName.

variable type is String

Now lets see what all functions have been provided by Scala for String class.

Type langName. (press Tab key)

You will see output like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

scala> langName.
+                     asInstanceOf          charAt              
codePointAt           codePointBefore       codePointCount      
compareTo             compareToIgnoreCase   concat              
contains              contentEquals         endsWith            
equalsIgnoreCase      getBytes              getChars            
indexOf               intern                isEmpty            
isInstanceOf          lastIndexOf           length              
matches               offsetByCodePoints    regionMatches      
replace               replaceAll            replaceFirst        
split                 startsWith            subSequence        
substring             toCharArray           toLowerCase        
toString              toUpperCase           trim                

Now lets use “toUpperCase” method from above list

Type langName.toUpperCase (enter)

You will see output like this

1
2
scala> langName.toUpperCase
res3: String = SCALA

Now as you can see above the upper-case string has been stored in new variable called res3.

To save the upper case variable in langName variable type

1
langName = langName.toUpperCase

You will see output like this

1
2
scala> langName = langName.toUpperCase
langName: String = SCALA

As you can see from above Scala interpreter

  1. Reads an expression

  2. Evaluates it

  3. Prints it

  4. goto 1

That is why the name REPL (read,evaluate,print,loop)

Scala program is not an interpreter, behind the scenes, our input transformed into byte code, and then the byte code runs via java virtual machine.

This post is licensed under CC BY 4.0 by the author.