Home » Scala language

Scala Literal, Variables and Data Type | Scala programming tutorial

In this tutorial, we are going to learn about the Scala literals, variables and data types, and declarations of various types of variables using different data types.
Submitted by Shivang Yadav, on June 10, 2019

1) Scala Data Types

Scala has the same set of data types as in Java. The traditional 14 data types are inherited as it is in Scala.

The Following are Valid Data Types in Scala.

S. No. Data Type Bit Size Range
1 Byte 8 -128 to 127
2 Short 16 -32768 to 32767
3 Int 32 -2147483648 to 2147483647
4 Long 64 -9223372036854775808 to 9223372036854775807
5 Float 32 IEEE 754 single-precision
6 Double 64 IEEE 754 double-precision
7 Char 16 Unicode : U+0000 to U+FFFF
8 String *Char Sequence
9 Boolean 1 true/ false
10 Unit *No Value
11 Null *Null / empty reference
12 Nothing *Subtype, includes no value
13 Any *any object
14 AnyRef *reference type

2) Literals in Scala

A literal is a value that can be assigned to a variable. Literals are basic constants that are assigned to the variable.

Types of literals

  • Integer Literals: Literals of type int or type Long. Integer literals can be declared using suffix L or I.
  •     Example:
        Decimal Literal : var i = 10L
        Hexadecimal Literal : var I = 0xFFF
    
  • Float Literals: Literals of type float or type double. They use Suffix f/F for float and D/d for double.
  •     Example:
        Float Literal : var i = 12.35f
        Double Literal : var I = 123.5445d
    
  • Character Literals: Unicode characters like 'f'
  • String Literal: Multiple character literal like 'Include'
  • Multi-Line Literal: Muti-line string Literal.
  •     Example:
        "Include Help
        is of the best 
        Programming tutor"
    
  • Boolean: A literal with any of two values, True/False.

3) Variables in Scala

A variable is some space in the memory that stores values. The memory space allocated to the variable is based on its data type.

Declaration of variables in Scala

In Scala, there are two types of variables 1) mutable (just like normal variables whose values can be changed during program execution) and 2) immutable (just like a constant whose value cannot be changed during program execution).

Mutable variables are declared by using the "var" keyword, and immutable variables are declared by using the "val" keyword.

Example:

    var I = 23 // this value can be changed
    val H = 12 //this value cannot be changed. 

Although variable data type is detected by Scala but you can explicitly define it.

    var I : string = "Include"
    val H : string = "Help"





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.