Scala - Data Types

In this tutorial, we will learn about the different data types in Scala with examples. By Shivang Yadav Last updated : April 02, 2023

Data Types

In a programming language, a data type is also known as type, tells the compiler about the type of data that is used by the programmer i.e. if we initialize a variable as a float then the compiler will give it 8 bytes of memory space and it will hold a decimal point value.

The Scala data types are taken as it is from Java and the storage and length are the same. There are many different types of data types in Scala. We have listed commonly used data type, their syntax and working example,

Types of Data Types

  1. Boolean
  2. Integer
  3. Float
  4. Double
  5. Char
  6. String
  7. Nothing
  8. Any
  9. anyRef
  10. anyVar

1) Boolean

  • This data type has only two values true or false.
  • It takes up the storage of 2 bytes.
  • The boolean data type is generally used for checking conditional statements and has a default value false.

Syntax

var bool : Boolean = false;

Scala Example of Boolean Data Type

object MyClass {
       def main(args: Array[String]) {
          var bool : Boolean = true; 
          println("The boolean value is " + bool);
          if(bool){
              println("IncludeHelp")
          }
       }
    }

Output

The boolean value is true
IncludeHelp

2) Integer

  • This data type is the most commonly used in programming.
  • Its values range from -2147483648 to 2147483647.
  • Storage needed to be allotted is 32 bit (4 bytes).
  • Commonly used for general numerical calculations and the default value is 0.

Syntax

var i : Int = 233;

Scala Example of Integer Data Type

object MyClass {
       def main(args: Array[String]) {
          var i : Int = 243; 
          println("The number is " + i);
  }
 }

Output

The number is 243

3) Float

  • This data type is the most commonly used in programming for the decimal point numbers.
  • Its values are IEEE 754 single-precision float.
  • Storage needed to be allotted is 32 bit (4 bytes).
  • Commonly used for general numerical calculations and the default value is 0.0F.

Syntax

var i : Float = 23.3f;

Scala Example of Float Data Type

object MyClass {
      def main(args: Array[String]) {
         var i : Float = 243.623f; 
         println("The Floating Point is " + i);
      }
   }

Output

The Floating Point is 243.623

4) Double

  • This data type is used to handle decimal point numbers that need larger value and more precision.
  • Its values are IEEE 754 double-precision float.
  • Storage needed to be allotted is 64 bit (8 bytes).
  • The default value is 0.0D.

Syntax

var i : Double = 23.3;
    OR
var i : Double = 23.3d;

Scala Example of Double Data Type

object MyClass {
      def main(args: Array[String]) {
         var doublevar : Double = 243.62343d; 
         println("The Double float variable's value is " + doublevar);
         
      }
   }

Output

The Double float variable's value is 243.62343

5) Char

  • This data type is used to handle character assignments stored as unsigned Unicode characters.
  • Its values range from Range:0 to 216-1 Unicode.
  • Storage needed to be allotted is 16 bit (2 bytes).
  • The default value is '\u000'.

Syntax

var ch : Char = 'I';

Scala Example of Char Data Type

object MyClass {
      def main(args: Array[String]) {
         var ch : Char = 'I';
         println("The character value of the variable ch is " + ch);

      }
   }

Output

The character value of the variable ch is I

6) String

  • The string data type is used to store a character sequence in a program.
  • Stings are a very common type of input and a string variable can accept a string of any length.
  • An in-depth explanation of string class is given in string in Scala.
  • The default value of the string variable is Null.

Syntax

var name : String = "IncludeHelp";
    OR
var name  = "IncludeHelp";

Scala Example of String Data Type

object MyClass {
      def main(args: Array[String]) {
         var name : String = "IncludeHelp";
         println("Hello Welcome to " + name);
         
      }
   }

Output

Hello Welcome to IncludeHelp

7) Nothing

In Scala, Nothing is treated as the subtype of all types in Scala. Based on this Nothing is a subtype of Char, Float, Int and all the other data type as mention in data types and variables in Scala article.

Now, where is Scala Nothing data Type used? It has found its applications when you need to return nothing and that too without an error.

Suppose, if a function returns something only if a condition is true otherwise Nothing is returned, then in the else case there can be an error. To avoid this Scala uses Nothing.

8) any

The any class is treated as the superclass of all classes in Scala. It is at the root of the class hierarchy in Scala. This means every class that exists in Scala is by default a sub-class of Scala.

The any class has two subclasses they are: 1) anyRef, 2) anyVar

9) anyRef

The anyRef class is the root class of all reference type in Scala.

10) anyVar

The anyVar class is the root class of all value classes in Scala.

All data classes are valued class and are a subclass of the anyVar class.

All the classes are categorized as follows :

  • Char , Byte , Short are subrange types.
  • Int , Long are integer types.
  • Float , Double are floating point types.




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.