Category : Scala | Sub Category : Scala Programs | By Prasad Bonam Last updated: 2020-10-07 14:18:06 Viewed : 545
val
is an immutable variable — like final
in Java — and should be preferredvar
creates a mutable variable, and should only be used when there is
a specific reason to use itExample:
Following example illustrates about Scala Variables and Data
Types.
Save the file as − DataTypes.scala.
DataTypes.scala //File name
package runnerdev
object DataTypes {
def main(args: Array[String]) {
//create variables without declaring their type
val x = 10 //immutable
val s = "test
string"
println("x " +
x)
println("s " +
s)
var a = 100 //mutable
a = 200
println("a " + a)
// You can also explicitly declare a
variable`s type
val y: Int = 1
val str: String = "This is a
string"
var z: Int = 11
println("y " + y)
println("z " + z)
println("str " + str)
// x=11 it gives error as reassignment to val
}
}
compile
and run the above example as follows
scala>
scalac DataTypes.scala
scala>
scala DataTypes
OutPut:
x 10
s test string
a 200
y 1
str This is a string
Scala supports a wide range of data types, including both primitive types and reference types. Here is an overview of the primary data types in Scala:
Primitive Types:
Byte: An 8-bit signed integer. Range: -128 to 127.
Short: A 16-bit signed integer. Range: -32,768 to 32,767.
Int: A 32-bit signed integer. Range: -2,147,483,648 to 2,147,483,647.
Long: A 64-bit signed integer. Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
Float: A 32-bit floating-point number, typically used for non-integer numeric values.
Double: A 64-bit floating-point number, used for floating-point numbers with higher precision.
Char: A 16-bit Unicode character, representing a single character within single quotes (e.g., `a`, `B`, `$`).
Boolean: Represents either true
or false
.
Reference Types:
String: A sequence of characters. Strings in Scala are immutable.
Symbol: A lightweight data type representing a unique, interned string. Symbols are often used as keys in maps.
Null: A type that has one value, null
, which is used to represent the absence of a value or a reference.
Unit: Similar to void
in some other languages, used to indicate the absence of a meaningful value. The Unit
type is returned from functions that have no interesting result.
Nothing: A subtype of all types. It represents a value that doesn`t exist or a program that doesn`t terminate normally.
Any Types:
Any: The supertype of all types in Scala, including both primitive and reference types.
AnyVal: The supertype of all value types, which includes the primitive types.
AnyRef: The supertype of all reference types.
In addition to these basic data types, Scala allows you to define your own custom data types using classes, traits, and case classes. This flexibility in creating custom data types makes Scala well-suited for modeling complex and structured data in your applications.
Remember that Scala also supports type inference, so you often do not need to explicitly specify the data type of a variable or expression, as the compiler can infer it from the context. This can lead to more concise and readable code.