1.
Introduction
2.
Types of Literals
2.1.
Integer Literals
2.2.
Float Point Literals
2.3.
String and Character Literals
2.4.
Boolean Literals
2.5.
Type Alias
3.
3.1.
What is the use of the let keyword in swift?
3.2.
What is the use of the var keyword in swift?
3.3.
What is the difference between var and let keywords?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

# Literals in Swift

## Introduction

In this article, we will be learning about the Literals in swift. Just like every other language (C/C++), swift also has a concept of literal. Literal is a direct constant value of a swift variable. It can be of type number, string, character, etc.

Syntax:

``let articleName = “Literals in Swift”``

Explanation: Here, "articleName" is the variable name and "Literals in Swift" is the constant value of the typed string assigned to the variable. Now Let's discuss the types of literal.

## Types of Literals

For every type of variable, there is a literal. In this section, we will discuss all kinds of literal.

### Integer Literals

Integer literals are also of many types here, we are going to discuss all of them.

• Binary: It is used to represent binary numbers, that is, numbers in 0 and 1. It starts with 0b.
• Octal Literals: It is used to represent octal values that are digits from 0 to 7. It starts with 0o.
• Hexadecimal: It is used to represent hexadecimal value. That is, digits are from 0 to 15. It starts with 0x.
• Decimal: It is used to represent decimal numbers. Anything that we write in integer form is the integer literals.

Example: Below example shows all kinds of integer literals discussed above.

Code:

``````//Binary Literals
let binaryNumber = 0b11111111
print(binaryNumber)

//Octal Literals
let octalNumber = 0o12376
print(octalNumber)

//Decimal
let decimalNumber = 12345
print(decimalNumber)``````

Output:

``````255
5374
15360.0
12345``````

### Float Point Literals

A floating-point literal comprises an integer portion, a decimal point, a fractional part, and an exponent part. Floating-point literals can be expressed in decimal or hexadecimal format.

A sequence of decimal digits is followed by a decimal fraction, a decimal exponent, or both in decimal floating-point literals.

A 0x prefix precedes an optional hexadecimal fraction, which a hexadecimal exponent follows in hexadecimal floating-point literals.

Example: In this example, we discuss the floating-point literals.

Code:

``````//Decimal double Literals.
let decimal = 30.375
print(decimal)

//Exponent double literals.
let exponent = 3.0375e1
print(exponent)

Output:

``````30.375
30.375
30.375``````

### String and Character Literals

A sequence of characters is called a string enclosed in double-quotes, while a character literal is a single character enclosed in double quotations.

Example: In this example, we will show the use of the above literal.

Code:

``````// Character Literal
let ch:Character = "C"
print(ch)

// String Literal
let str:String = "This is coding ninjas's Coding Ninjas Studio."
print(str) ``````

Output:

``````C
This is coding ninjas's Coding Ninjas Studio.``````

### Boolean Literals

In Swift, the Bool variable represents Boolean values. Create Bool instances by assigning the result of a Boolean method or operation to a variable or constant or using one of the Boolean literals, true or false.

Example: Showing boolean literals.

Code:

``````let var1:Bool = false
print(var1)

let var2:Bool = true
print(var2)``````

Output:

``````false
true``````

### Type Alias

A type alias allows you to give an existing data type a new name in your program. Following the declaration of a type alias, the aliased name can be used in place of the existing type throughout the program. The use of type aliases does not result in the creation of new types. They merely give an existing type a new name.

Example: Showing the use of type alias.

Code:

``````// Creating typealias.
typealias myOwn = String

//Creating variable of our type alias.
var series: myOwn = "Game of Thrones"
print(series)  ``````

Output:

``Game of Thrones``

### What is the use of the let keyword in swift?

In Swift, we use the let keyword to construct immutable variables. An immutable variable is a constant that can only be initialized once.

### What is the use of the var keyword in swift?

To declare a variable in Swift, we utilize the var keyword. Variables are used in Swift to store and refer to values by their names. Before they can be utilized, variables must be declared.

### What is the difference between var and let keywords?

After a var variable has been initialized, it can be altered. On the other hand, a let variable cannot be modified after it has been created. Attempting to reassign, reinitialize, or mutate a Swift let variable will result in a compiler error, similar to const variable in other languages.

## Conclusion

In this article, we have extensively discussed the Literals in swift. We discussed the different types of literal along with their examples.

We hope this blog has helped you enhance your knowledge of the LiteralsCheck out the awesome content on the Coding Ninjas Website, Swift Environment setupSwift setsSwift dictionarySwift Online ResourcesCoding Ninjas Studio ProblemsCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview ExperiencesCoding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test SeriesDo upvote our blog to help other ninjas grow.

Happy Coding!

Live masterclass