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.

13. SCALA – STRINGS<br />

Scala<br />

This chapter takes you through the Scala Strings. In Scala, as in Java, a string is an immutable<br />

object, that is, an object that cannot be modified. On the other hand, objects that can be<br />

modified, like arrays, are called mutable objects. Strings are very useful objects, in the rest<br />

of this section, we present important methods of java.lang.String class.<br />

Creating a String<br />

The following code can be used to create a String:<br />

var greeting = "Hello world!";<br />

or<br />

var greeting:String = "Hello world!";<br />

Whenever compiler encounters a string literal in the code, it creates a String object with its<br />

value, in this case, “Hello world!”. String keyword can also be given in alternate declaration<br />

as shown above.<br />

Try the following example program.<br />

object Demo {<br />

val greeting: String = "Hello, world!"<br />

}<br />

def main(args: Array[String]) {<br />

}<br />

println( greeting )<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 />

88

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

Saved successfully!

Ooh no, something went wrong!