Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Scala Literals

Scala Literals

In Scala, literals are expressions that directly represent fixed values.

Scala supports various types of literals, covering numbers, characters, strings, booleans, and more.

Integer Literals

Integer literals are used for the Int type; to represent a Long, you can add the suffix L or lowercase l after the number.

Integer literals can be decimal, hexadecimal, or octal.



val decimal = 42

val hex = 0x2A

Floating-Point Literals

If a floating-point number has an f or F suffix, it represents a Float type; otherwise it is a Double.



val doubleValue = 3.14

val floatValue = 3.14f

Boolean Literals

Boolean literals are true and false.

val isScalaFun = true

val isSkyGreen = false

Symbol Literals

A symbol literal is written as: ' , where can be any alphanumeric identifier (note: it cannot start with a digit). Such a literal is mapped to an instance of the predefined class scala.Symbol.

For example: the symbol literal 'x is shorthand for the expression scala.Symbol("x") , the symbol literal is defined as follows:



package scala

final case class Symbol private (name: String) {

   override def toString: String = "'" + name

}

Character Literals

In Scala, a character variable is defined using single quotes ', as follows:

'a' 

'\u0041'

'\n'

'\t'

where ** represents an escape character, which can be followed by u0041 a digit or \r\n and other fixed escape characters.

String Literals

In Scala, a string literal is defined using double quotes ", as follows:

"Hello,\nWorld!"

"Runoob Tutorial官网:www.runoob.com"

Representation of Multiline Strings

A multiline string uses three double quotes as delimiters, in the format: """ ... """.

The example is as follows:



val foo = """Runoob Tutorial

www.runoob.com

www.runnoob.com

以上三个地址都能访问"""

Null Value

Null is a special literal representing the empty value of all reference types.

The empty value is of type scala.Null.

Scala.Null and scala.Nothing are special types that handle certain "edge cases" of Scala's object-oriented type system in a unified way.

The Null class is the type of the null reference object; it is a subclass of every reference class (classes inheriting from AnyRef). Null is not compatible with value types.

val nullValue: String = null

Unit

The Unit type has a single instance value, represented by the literal (), similar to void in Java.



val unitValue: Unit = ()

Example

The following is example code using different types of literals:

Example

object

LiteralsExample

{

 

def

main

(

args

:

Array

[

String

]

)

:

Unit

=

{

   

// numeric literals

   

val

decimal

=

42

   

val

hex

=

0x2A

   

val

doubleValue

=

3.14

   

val

floatValue

=

3.14f

   

   

// character literals

   

val

charA

=

'A'

   

val

charNewLine

=

'\n'

   

   

// string literals

   

val

greeting

=

"Hello, World!"

   

val

multilineString

=

""

"This is
                            |a multi-line
                            |string"

""

.

stripMargin

   

   

// boolean literals

   

val

isScalaFun

=

true

   

val

isSkyGreen

=

false

   

   

// special literals

   

val

nullValue

:

String

=

null

   

val

unitValue

:

Unit

=

(

)

   

   

// sample output

    println

(

s

"Decimal: $decimal, Hex: $hex"

)

    println

(

s

"Double: $doubleValue, Float: $floatValue"

)

    println

(

s

"Char: $charA, New Line: $charNewLine"

)

    println

(

s

"Greeting: $greeting"

)

    println

(

s

"Multiline String: $multilineString"

)

    println

(

s

"Is Scala Fun: $isScalaFun, Is Sky Green: $isSkyGreen"

)

    println

(

s

"Null Value: $nullValue, Unit Value: $unitValue"

)

 

}

}

The result of compiling and executing the above code is:

Decimal: 42, Hex: 42

Double: 3.14, Float: 3.14

Char: A, New Line: 



Greeting: Hello, World!

Multiline String: This is

a multi-line

string

Is Scala Fun: true, Is Sky Green: false

Null Value: null, Unit Value: ()