×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

Scala Numeric Data Types

By IncludeHelp Last updated : October 07, 2024

Numeric type in Scala deals with all types that are stored in numerical. The data type that is used is decimals (float and Double) and integers (Int, Short, Long).

1) Int Data Type in Scala

The int data type stores integer variables that take memory location of 4 bytes. The value of int data type ranges from -2147483648 to 2147483647.

Syntax to declare an Int variable

var a = 343 // Without date type
var a : Int = 4623 // With data type 

Scala Example of Int Data Type

object MyClass {
      def main(args: Array[String]) {
        var ch = 324
        var b : Int = 3342
        println("The value of int ch is " + ch)
        println("Value of integer b is " + b)
      }
   }
Output
The value of int ch is 324
Value of integer b is 3342

2) Short Data Type in Scala

The short data type stores the integer value of variables that takes memory location of 2 bytes. The value of short data type ranges from -32768 to 32767.

Syntax to declare a Short variable

var a = 343 // Without data type will give int 
var a : Short = 4623 //without this variable will be integer. 

Scala Example of Short Data Type

object MyClass {
      def main(args: Array[String]) {
        var b : Short = 3342;
        println("Value of short integer b is " + b);
      }
   }
Output
Value of short integer b is 3342

3) Long Data Type in Scala

The long data type stores integer value to variables that takes memory location of 8 bytes. The value of long data type ranges from -9223372036854775808 to 9223372036854775807.

Syntax to declare a Long variable

var a = 343 // Without date type will give int 
var a : Long = 4623 //without this variable will be integer. 

Scala Example of Long Data Type

object MyClass {
      def main(args: Array[String]) {
        var b : Long = 33423452;
        println("Value of long integer b is " + b);
      }
   }
Output
Value of long integer b is 33423452

4) Float Data Type in Scala

The float data type stores decimal values to its variables that takes memory location of 4 bytes. The value of float data type ranges from -3.4E+38 to +3.4E+38 i.e. single precision.

Syntax to declare a Float variable

var variable_name = 343.43 
var variable_name : Float = 463.876 

Scala Example of Float Data Type

object MyClass {
      def main(args: Array[String]) {
        var b : Float = 33.97f;
        println("Value of Float value b is " + b);
      }
   }
Output
Value of Float value b is 33.97

5) Double Data Type in Scala

The double data type stores decimal values to its variables that takes memory location of 8 bytes. The value of float data type ranges from IEEE 754 double-precision float.

Syntax to declare a Double variable

var variable_name = 3463.434565 // this will be initialized as float
var variable_name : Double = 46763.87634

Scala Example of Float Data Type

object MyClass {
      def main(args: Array[String]) {
        var b : Double = 345453.978765;
        println("Value of Double value b is " + b);
      }
   }
Output
Value of Double value b is 345453.978765

Comments and Discussions!

Load comments ↻





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