Javatpoint Logo
Javatpoint Logo

Scala String

In scala, string is a combination of characters or we can say it is a sequence of characters. It is index based data structure and use linear approach to store data into memory. String is immutable in scala like java. You can manipulate string and can apply method to get desire result but you can't change original string object.


Scala String Example

This is a simple string example in which we have created a string variable.

Output:

Scala string example

Scala Immutable String Example 1

In this example, we are creating a string variable and trying to modify string object. Let's see what happen.

Output:

Scala string example

Scala Immutable String Example 2

In this example, we are modifying the string and assigning it to a variable so that we can keep reference of modified string. Here, a new string object is created after modification. The s1 variable now refers to new string object. The old string object is still available in the memory but does not have any reference.

Output:

This is Scala string example 

Scala String Comparison Example

In scala, you can compare two string objects by using == (equal) method. The following program describes how to use equal operator. It returns boolean value either true or false.

Output:

false
true




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


B.Tech / MCA