Scala string
The following example assigns a string to a constant:
Example
object Test {
val greeting: String = "Hello,World!"
def main(args: Array[String]) {
println( greeting )
}
}
The above example defines the variable greeting
is a string constant oftype String(java.lang.String)
.
In Scala, the type of string is actually Java String
, it doesn’t have its own String
class.
In Scala String
is an immutable object, so the object cannot be modified. This means that if you modify the string, a new string object willbe generated.
But other objects, such as arrays, are mutable objects. Next, we will introduce to you the commonly used ones java.lang.String
method.