How to create different types of variables in scala programming? In this post, we are especially going to see how you can create the different variables and how to assign values to each of them.
Scala Program To Create Different Types Of Variable
In this program, we took 4 different data types. We declared each variable for data types and then assigned a value to it.
object divineseo{
def main(args:Array[String]){
var b:Boolean = false
var i:Int = 123456789
var f:Float = 9.19999F
var s:String = "Hello World"
var c:Char = 'A'
println ("Boolean: "+b)
println ("Int: "+i)
println ("Float: "+f)
println ("String: "+s)
println ("Char: " +c)
}
}
Data Types | Description |
---|---|
Boolean | Either the literal true or the literal false |
Int | 32-bit signed value (Range -2147483648 to 2147483647) |
Float | 32-bit IEEE 754 single-precision float |
String | It is a sequence of Chars |
Char | 16-bit unsigned Unicode character (Range from U+0000 to U+FFFF) |
Output
Boolean: false
Int: 123456789
Float: 9.19999
String: Hello World
Note: The first letter of any data type should be in a capital letter otherwise it produces an error
error: not found: type int
Scala program to print the value of variables using printf() function
As we all are familiar with printf(), it is used in C programming to print the statement or value or result in the console screen. Now we are going to use printf() function to print the values in the console screen in scala programming.
object divineseo{
def main(args:Array[String]){
var b:Boolean = false
var i:Int = 123456789
var f:Float = 9.19999F
var s:String = "Hello World"
var c:Char = 'A'
printf ("Boolean is %b\n",b)
printf ("Integer is %d\n", i)
printf ("Float is %f\n", f)
printf ("String is %s\n", s)
printf ("Char is %c\n", c)
}
}
It is very much similar to C programming because we are using the same line of code if we wish to print something in c language. But in scala we do not use “;” to terminate the line.