Click here to Skip to main content
16,018,394 members
Articles / Programming Languages / Scala

Scala Types / Variables

Rate me:
Please Sign up or sign in to vote.
4.88/5 (5 votes)
15 Oct 2015CPOL3 min read 19.2K   3   2
Scala types / variables

Since Scala is a JVM language, it is not surprising to see that it has the same data types as Java.

The following table illustrates the common data types:

Data Type Description
Byte 8 bit signed value. Range from -128 to 127
Short 16 bit signed value. Range -32768 to 32767
Int 32 bit signed value. Range -2147483648 to 2147483647
Long 64 bit signed value. -9223372036854775808 to 9223372036854775807
Float 32 bit IEEE 754 single-precision float
Double 64 bit IEEE 754 double-precision float
Char 16 bit unsigned Unicode character. Range from U+0000 to U+FFFF
String A sequence of Chars
Boolean Either the literal true or the literal false
Unit Corresponds to no value
Null null or empty reference
Nothing The subtype of every other type; includes no values
Any The supertype of any type; any object is of type Any
AnyRef The supertype of any reference type

The items in this table are all objects, which means you can call methods on them.

For example, suppose we have declared an int value like this:

Java
var _int = 23 

We could then call methods on it like this:

image

This is somewhat different to Java where there are true primitives. Scala's approach uses objects known as RichXXX which have something called implicit conversions, which allows them to be used to interop with Java where you may need to pass a Java primitive (say as int).

For example, there is a RichInt class in Scala, which you can read about here:

There are quite a few RichXXX classes in Scala, I would urge you all to read about them.

There is a good discussion on this at StackOverflow:

For now, let's see how we can declare some variables in Scala.

Integers

Here are some valid ways to create integers:

Java
val _int1 = 0
val _int2 = 35
val _int3 = 21
val _int4 = 0xFFFFFFFF
val _int5:Long = 0XCAFEBABE

Floating Points

Here are some valid ways to create floating points:

Java
val _float1 = 0.0
val_float2 = 1e40f
val _float3 = 2.1543f
val _float4 = 6.0e600
val _float5 = .4

Booleans

Here are some valid ways to create booleans:

Java
val _bool1 = false
val _bool2 : Boolean = true
val _bool = _bool1 && _bool2

Characters

Here are some valid ways to create characters:

Java
val _char1 = 'b'
val _char2 = '\u0037'
val _char3 = '\n'
val _char4:Char = '\t'

Strings

Here are some valid ways to create strings:

Java
val _string1 = "Hello,\nWorld!"
val _string2 = "This string contains a \" character."

Multiline strings

You can also create multiline strings like this:

Java
val _multiLineString ="""this string
                          is a long one, it has 
                          3 lines."""

String interpolation

Scala also supports string interpolation, such that you can do things like this:

Java
val _string1 = "Hello,\nWorld!"
val _string2 = "This string contains a \" character."
val string3 = s"The value of string1 = $_string1, and string2 = $_string2"

Escape Sequences

The following escape sequences are valid:

Escape Sequence Unicode Description
\b \u0008 backspace BS
\t \u0009 horizontal tab HT
\n \u000c formfeed FF
\f \u000c formfeed FF
\r \u000d carriage return CR
\” \u0022 double quote “
\’ \u0027 single quote .
\\ \u005c backslash \

Var vs Val

There is one last thing I wanted to mention in this post, which is the difference between var and val in Scala.

Val

Basically val is an immutable object. That is you can change the value of it, but you will not be able to recreate a new object pointer (reference) for a val. This would be an error. An object assigned to a var can still have its internal state altered though.

Var

Var on the other hand is mutable, and as such will let you assign a new object pointer (reference).

There is a great StackOverflow post on this, which I would encourage you all to read:

This article is part of the series 'Scala View All

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions

 
GeneralMy vote of 5 Pin
Farhad Reza15-Oct-15 20:31
Farhad Reza15-Oct-15 20:31 
Nice
GeneralRe: My vote of 5 Pin
Sacha Barber15-Oct-15 21:18
Sacha Barber15-Oct-15 21:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.