Friday, 14 August 2020

Scala: Define a string

 You can define a string using ‘String’ type.

Syntax

var variable_name : String = "message"

 

Example

var msg : String = "Hello, how are you

scala> var msg : String = "Hello, how are you"
var msg: String = Hello, how are you

scala> print(msg)
Hello, how are you

Since Scala infers type, you can omit String type while defining the variable.

scala> var name = "Krishna"
var name: String = Krishna

As you see, I defined the variable ‘name’ but not specified type. Scala infers the type automatically depends on the value it holds.



Previous                                                    Next                                                    Home

No comments:

Post a Comment