Scala Variables and Data Types program

Category : Scala | Sub Category : Scala Programs | By Prasad Bonam Last updated: 2020-10-07 14:18:06 Viewed : 591


Scala Variables and Data Types program

 Scala has two types of variables:

  •            val is an immutable variable — like final in Java — and should be preferred
  •            var creates a mutable variable, and should only be used when there is a specific reason to use it

 

Example:

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:

  1. Byte: An 8-bit signed integer. Range: -128 to 127.

  2. Short: A 16-bit signed integer. Range: -32,768 to 32,767.

  3. Int: A 32-bit signed integer. Range: -2,147,483,648 to 2,147,483,647.

  4. Long: A 64-bit signed integer. Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

  5. Float: A 32-bit floating-point number, typically used for non-integer numeric values.

  6. Double: A 64-bit floating-point number, used for floating-point numbers with higher precision.

  7. Char: A 16-bit Unicode character, representing a single character within single quotes (e.g., `a`, `B`, `$`).

  8. Boolean: Represents either true or false.

Reference Types:

  1. String: A sequence of characters. Strings in Scala are immutable.

  2. Symbol: A lightweight data type representing a unique, interned string. Symbols are often used as keys in maps.

  3. Null: A type that has one value, null, which is used to represent the absence of a value or a reference.

  4. 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.

  5. Nothing: A subtype of all types. It represents a value that doesn`t exist or a program that doesn`t terminate normally.

Any Types:

  1. Any: The supertype of all types in Scala, including both primitive and reference types.

  2. AnyVal: The supertype of all value types, which includes the primitive types.

  3. 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.

Search
Related Articles

Leave a Comment: