Literal
A literal is data that appears directly in the source code, like number5
,
character A
and text "Hello, World"
.
Type
A type is the definition or classification of stored data such as anInt
or Char
. All
data in Scala corresponds to a specific type. Scala types are object-based (aka
object-oriented) and provide functions for operating with their kind of data.Specifying the type in value definitions is optional. In situations where it is possible to deduce the type of the value based on its assignment (for example, the literal 10 is obviously an integer) we can leave off the type from a value definition like
val x = 10
. The Scala compiler will then
discern the type of the value from its assignment, this process known as type inference.
Scala never have a value defined without a type. The values defined without type are assigned the proper type by
Scala compiler just as if the type had been included in the definition.Although type inference will deduce the correct type to use to store a literal value, it will not override a defined explicit type. If we define a value with a type that is incompatible with the initial value compiler throws a compilation error in such scenario like
val x: Int = "Scala"
.
-
Scala has both numeric
Int and Double
and non-numeric typesString
that can be used to define values and variables. - These core types are the building blocks for all other types including objects and collections
- Each type in Scala is an object that have methods and operators which act on their data
- Unlike Java and C there is no concept of a primitive type in Scala
- Unlike Java, Scala supports the ability to automatically convert numbers from one type to another based on the rank of the type (e. g. Int to Long, Long to Double etc.)
- Scala does not allow automatic conversion from higher ranked types to lower ranked types (e. g. Long to Int, Double to Long etc.). It allows us to convert between types manually using the toType methods available on all numeric types. Although this makes it possible to lose data by converting to a lesser ranked type, it is useful when we know that the data is compatible with the lower ranked type.
Value & Variable
Value
A value is an immutable (i. e. unmodifiable), named, and typed storage unit for data. Values are defined with the val keyword (which is why you will often hear Scala developers refer to values as val’s).Scala values are defined with the syntax
val <name> [:<type>] = <literal>,
so
to create a value with the name x, type Int (short for Integer) and
assigned with a literal number 5
, we use val x: Int = 5
.
Variable
A variable is a named and typed storage unit for data, but unlike values can be reassigned other data over and over again. Variables are defined with the var keyword.Scala variables are defined with the syntax
var <name>[:<type>] = <literal>
so to create a variable with the name x, type Int (short for "Integer")
and assigned with a literal number "5", we use var x: Int = 5
.
Variable v/s Value
Scala re-assignable variables are not the necessarily the default pattern for storing data. Values and variables are the two methods of storing data in Scala, and among experienced developers, it is values that are preferable because of the stability and predictability they bring to source code.- Value will retain the same value regardless of any other code that may access it.
- Reading and debugging code is easier when a value assigned at the beginning of a code segment can be assured of retaining its original value through the end of the code segment.
- When working with data that may be available for the lifespan of an application, or accessible from concurrent or multi-threaded code, an immutable value will be more stable and less prone to errors than mutable data that may be modified at unexpected times.
String Interpolation
Building a String based on other values is reasonably easy to do with string addition.Direct approach
e .g. "Pi, using 355/113, is about " + approx + "."
Combine values or variables
A more direct way to combine values or variables inside a String is with string interpolation, a special mode where external value and variable names are recognized and resolved.- The Scala notation for string interpolation is a "s" prefix added before the first double quote of the string. Then dollar sign operators ($) (with optional braces) can be used to note references to external data
- e .g.
s"Pi, using 355/113, is about $approx."
printf notation
An alternate format for string interpolation uses printf notation, very useful when we want to control the data formatting such as the character count or display of fractions.- To use printf notation change the prefix to an "f" and follow the end of the reference immediately with the printf notation.
- e .g.
f"Pi, using ${355/113.0}%.5f, is about $approx."
Scala’s Type Hierarchy
All of Scala’s types, from numbers to strings to collections, exist as part of a type hierarchy. Every class that we define in Scala will also belong to this hierarchy automatically.-
The
Any
,AnyVal
andAnyRef
types are the root of Scala’s type hierarchy -
Any
is the absolute root, and all other types descend from its two children,AnyVal
andAnyRef
. -
The types that extend
AnyVal
are known as value types because they are the core values used to represent data. -
All other types have
AnyRef
as their root and are only allocated on the heap as objects, thus the termRef
indicating that they are reference types that are accessed via a memory reference. -
Nothing
is a subtype of every other type and exists to provide a compatible return type for operations that significantly affect a program’s flow. -
Null
, a subtype of all AnyRef types that exists to provide a type for the keyword null, the value of all unallocated reference types. Except that whereas null would be a keyword in many other languages, in Scala it is the one and only instance of Null and an example of how Scala’s syntax prefers the use of real types and instances to reserved keywords. -
The
Unit
type is unlike the other core types here (numeric and non-numeric) in that instead of denoting a type of data it denotes the lack of data. In a way it is similar to thevoid
keyword used in Java and C.
1 Comments
Nice article also visit for more details
ReplyDeletehttp://www.javaproficiency.com/2015/11/scala-variables.html
Post a Comment