28.08.2016 Views

scala_tutorial

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Scala<br />

var floatVar = 12.456<br />

var intVar = 2000<br />

var stringVar = "Hello, Scala!"<br />

var fs = printf("The value of the float variable is " +<br />

"%f, while the value of the integer " +<br />

"variable is %d, and the string " +<br />

"is %s", floatVar, intVar, stringVar)<br />

println(fs)<br />

}<br />

}<br />

Save the above program in Demo.<strong>scala</strong>. The following commands are used to compile and<br />

execute this program.<br />

\><strong>scala</strong>c Demo.<strong>scala</strong><br />

\><strong>scala</strong> Demo<br />

Output<br />

The value of the float variable is 12.456000, while the<br />

value of the integer variable is 2000, and the<br />

string is Hello, Scala!()<br />

String Interpolation<br />

String Interpolation is the new way to create Strings in Scala programming language. This<br />

feature supports the versions of Scala-2.10 and later. String Interpolation: The mechanism<br />

to embed variable references directly in process string literal.<br />

There are three types (interpolators) of implementations in String Interpolation.<br />

The ‘s’ String Interpolator<br />

The literal ‘s’ allows the usage of variable directly in processing a string, when you prepend<br />

‘s’ to it. Any String variable with in a scope that can be used with in a String. The following<br />

are the different usages of ‘s’ String interpolator.<br />

The following example code snippet for the implementation of ‘s’ interpolator in appending<br />

String variable ($name) to a normal String (Hello) in println statement.<br />

91

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!