Scala tutorials
Scala Data Types and Variables
Scala has all data types available to Java as well as its own, in Scala there are different variables and types. In this article we will look in detail at Scala data types and variables.
Below are few data types in Scala:
SN | Data Type | Description |
1 | Byte | 8 bit signed value. Range from -128 to 127 |
2 | Short | 16 bit signed value. Range -32768 to 32767 |
3 | Int | 32 bit signed value. Range -2147483648 to 2147483647 |
4 | Long | 64 bit signed value. -9223372036854775808 to 9223372036854775807 |
5 | Float | 32 bit IEEE 754 single-precision float |
6 | Double | 64 bit IEEE 754 double-precision float |
7 | Char | 16 bit unsigned Unicode character. Range from U+0000 to U+FFFF |
8 | String | A sequence of Chars |
9 | Boolean | Either the literal true or the literal false |
10 | Unit | This means void or nothing. |
11 | Null | Null or empty |
12 | Nothing | Nothing data type of Scala |
13 | Any | This is higher level, means can take any data type. |
14 | AnyRef | Super type of Any |
Escape Sequence In Scala
Below are escape sequences in Scala:
SN | Escape Seq | Unicode/Code | Details |
1 | \b | \u0008 | backspace BS |
2 | \t | \u0009 | horizontal tab HT |
3 | \n | \u000c | formfeed FF |
4 | \f | \u000c | formfeed FF |
5 | \r | \u000d | carriage return CR |
6 | \” | \u0022 | double quote “ |
7 | \’ | \u0027 | single quote . |
8 | \\ | \u005c | backslash \ |
Variables In Scala
In any programming language we use variables to hold certain values, some numeric, float or string values, same variables exist in Scala as well, they hold certain data types and values in memory. Based on data types variable may have different size and memory requirement for example an Int will take less memory than a Float.
Variable Declaration
We can specify variable name and type and then a value to variable that is how variable is declared. Also there is other form where we don’t need so specify type and still can declare variable, let’s see the syntax of both:
Variable with type syntax:
val <Variable type> = <Variable value>
Variable without type syntax:
val <Variable type> = <Variable value>
Variable Type Inference
Scala compiler does not need developer to specify variable types while declaration and when value is assigned to it at that time it determines type of variable. We already saw a above declaration.
Example:
val age = 20
val name = “Jonny”
Val vs Var in Scala
Val is a keyword used to declare variable as immutable/final, in case of object it makes them singleton in nature and such variables can’t be reassigned.Var is a keyword used to declare normal variables which are mutable and can be reassigned with new values.
Examples of Variables:
object VariableExmaple { def main(args: Array[String]): Unit = { //Mutable variables var age = 10 var amount = 20.24f age = age +20 var name = "Jonny" name = name + " and Rong" var message = "Hello " + name //Immutable variables val immutableAge = 10 //We cant change values of these val immutableName = "Jonny English" //We cant change values of these println("Mutable variables age: "+age+" Name: "+name + " Message: "+message) println("Immutable variables age: "+immutableAge+" Name: "+immutableName) } }
Output:
Mutable variables age: 30 Name: Jonny and Rong Message: Hello Jonny and Rong
Immutable variables age: 10 Name: Jonny English
What is Tuple variable in Scala
Tuple are special variables which can be created easily by declaring multiple variables in one, it allows to have a maximum 22 variables in one tuple.
Example of Tuples:
val myDetail = (10, "Jonny")
val myAddress = ("ZZZ Street", "XYZ City", "ABC Country")
In the above example both variables are tuples, myDetail variable has two different types of variable first Int and second String.
myAddress variable has three variables of String type. We don’t need to declare type as Scala automatically infers type based on assigned values.
How to Use Tuples
We can use tuples by name of tuple then dot (.) and then _<number of variable which we want to access>.
Syntax:
<tuple variable name>._<Number of variable which we want to access in tuple>
Example of Tuples and Use
object TuplesExample {
def main(args: Array[String]): Unit = {
val myDetail = (10, "Jonny")
val myAddress = ("ZZZ Street", "XYZ City", "ABC Country")
println("Tuples values myDetail: "+myDetail._1 +" "+myDetail._2)
println("Tuples values myAddress: "+myAddress._1 +" "+myAddress._2+" "+myAddress._3)
}
}
References
Reference from Scala org: Variables
Next Articles
- Scala Classes and Objects