Online Tutorials & Training Materials | STechies.com
Register Login

Code Example- Getting Started with Scala

|| 0

Code Example- Getting Started with Scala
Stechies

 First Scala Program 'Hello World'

1. In Java, if you want to print 'Hello World' then you have to write the following lines, which is around 5-6 lines of code.

class Demo
{
public static void main(String a [])
{
System.out.println("Hello World");
}
}

2.Go to the Scala window and create a worksheet. Click on SRC > New > scala worksheet. Then give a name to the worksheet.

3.Go to Window > Show view > scala interpreter. Then select the required Scala project to work upon.

4.Select the Scala interpreter window.

5.Next type “Hello world” in the text window. Press CTRL + Enter to view the output.

Create variables in Scala

6.To create a variable in java, you need the following code.

7.Go back to the Scala window. Type var i = 9 .Here you don’t have to specify the variable type like in java (int).

8.You will get the output where Scala will detect the variable type on its own.

Specify variable type in Scala

9.To specify a variable type in Scala, take the example of the following code

10.To get the output for the above code, press CTRL + Enter.

Perform action in Scala

11.Enter i+num in the text area. Then press CTRL+ Enter to view the output.

Create string variable

12.Enter the example as shown below. Then press CTRL+ Enter to view the output.