How we can declare mutable and immutable string in Kotlin?

By Arindam Kundu

How we can declare mutable and immutable string in Kotlin?


In Kotlin whatever value you define here that determines the data types of the variable. Suppose (var myNumber =10),that automatically becomes integer value. Similarly, you can write the datatypes of the variable (var myString: String). In case of the mutable string, you can modify string at any point of time. Example of mutable & immutable string-

fun main(args: Array<String>) {

var myString: String               // Mutable String
myString = “Hello World”
myString = “Another World”

val myAnotherString = “My Constant String”            //  Immutable String

myAnotherString = “Arin”           ……Error will be given in this line because Immutable String cannot be modified



String Interpolation :-

In Kotlin String interpolation process is little bit different from any other languages.Example-

fun main(arg: Array<String>) {

var rect = Rectangle()           //  creating object of Rectangle class named rect
rect:length = 5
rect:breadth = 3

print(” length = ${rect.length} and breadth = ${rect.breadth}. Area is ${rect.length * rect.breadth}”)


class Rectangle {

var length: Int = 0
var breadth: Int = 0


Output :

length = 5 and breadth = 3. Area is 15

Arindam Kundu Subscriber
Android Developer , Openweb Solutions

Android Developer at Openweb Solutions

Posts created 5

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top