Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Variable Type Inference | Variables and Data Types
Introduction to Scala
course content

Conteúdo do Curso

Introduction to Scala

Introduction to Scala

1. Getting Started
2. Variables and Data Types
3. Conditional Statements and Loops
4. Arrays
5. Strings

Variable Type Inference

Understanding Type Inference

While we have previously mentioned that when creating a variable in Scala you have to specify its data type explicitly, it's not an obligatory step. In fact, Scala compiler can automatically deduce the data type of the variable which is known as type interference.

In contrast, Java doesn't have such feature, so you should always manually specify the data types.

Let's now take a look at an example:

java

Main

copy
1
val myLetter = 'A'

As you can see, we did not specify the data type explicitly here. However, the compiler will automatically deduce that the data type of myLetter is Char. That's actually self-evident since only Char data type is a single character enclosed in single quotes.

Let's now take a look at another example:

java

Main

copy
1
val age = 18

Here, the value of age is 18 meaning that it can be either of type Byte, Short, Int or Long, so how will the compiler deduce which data type is "right" for this variable? Here is when default data types come into play.

Default Data Types

In Scala, when the compiler performs type inference, it assigns default data types based on the values provided. Here's an overview of the default data types used in common scenarios:

Integers

For integer values, Scala infers the type as Int. If the integer literal exceeds the Int range, the compiler will raise an error telling that the number is too large. In this case, you should append L at the end of the number to let the compiler know that this variable should be of Long type.

Let's take a look at an example to make things clear:

java

Main

copy
123
val age = 18 // Infers the type as Int val largeNumber = 6324523453L // Infers the type as Long val largeNumber2 = 6324523453 // An error will be raised

Floating Point

For floating-point numbers, Scala defaults to Double. However, in case you want a Float, you should append F or f at the end of the number.

java

Main

copy
12
val piDouble = 3.14 // Infers the type as Double val piFloat = 3.14F // Infers the type as Float

With Char and Boolean data types everything is rather simple. A single character enclosed in single quotes is inferred to be of type Char, while a variable with either true or false value is inferred to be of type Boolean.

java

Main

copy
12
val myLetter ='b' // Infers the type as Char val myBoolean = true // Infers the type as Boolean

Overall, it is sometimes better to specify the data types explicitly for integers and numbers with a decimal point, especially when you want the code to be memory efficient.

Tudo estava claro?

Seção 2. Capítulo 3
We're sorry to hear that something went wrong. What happened?
some-alt