Lesson 5 of 34
In Progress

# Data Types

In Solidity, data types are used to specify the type of data that a variable or constant can hold. Understanding the different data types available in Solidity is essential for building effective contracts and decentralized applications. In this article, we’ll take a closer look at the various data types available in Solidity and how they can be used in your code.

## Numeric Data Types

Solidity offers a range of numeric data types, including integers, fixed-point numbers, and unsigned integers. Here are some of the most commonly used numeric data types in Solidity:

• uint: Unsigned integer (positive whole number)
• int: Signed integer (positive or negative whole number)
• uint8 to uint256: Unsigned integer of 8 to 256 bits
• int8 to int256: Signed integer of 8 to 256 bits
• fixed: Fixed-point number
• fixed8x8 to fixed256x256: Fixed-point number with 8 to 256 bits before and after the decimal point

For example, the following code declares an unsigned integer called “myNumber” that can hold a value between 0 and 2^256-1:

uint256 myNumber;

## Boolean Data Type

Solidity also offers a boolean data type, which can hold the values true or false. The boolean data type is commonly used to represent the results of logical operations or to control the flow of a program.

For example, the following code declares a boolean called “isValid” and sets its value to true:

bool isValid = true;

## String Data Type

In Solidity, the string data type is used to represent a sequence of characters. Strings are commonly used to store text data, such as names, addresses, and descriptions.

To declare a string variable in Solidity, use the “string” keyword followed by the variable name. For example:

string myString;

This code declares a string called “myString”.

## Arrays

In Solidity, arrays are used to store a list of items of the same type. Arrays can be of any data type, including integers, booleans, and strings.

To declare an array in Solidity, use the data type of the items followed by square brackets and the variable name. For example:

uint[] myArray;

This code declares an array of unsigned integers called “myArray”.

## Mappings

Mappings in Solidity are used to store key-value pairs, similar to a dictionary in other languages. Mappings can be of any data type, and the keys can be of any data type that is hashable (able to be converted to a unique fixed-size value).

To declare a mapping in Solidity, use the “mapping” keyword followed by the key data type, an arrow, and the value data type, and then the variable name. For example:

mapping(address => uint) balances;

This code declares a mapping called “balances” that maps addresses (the keys) to unsigned integers (the values).

## Structs

Structs in Solidity are used to define custom data types that can be composed of several different types. Structs are useful for organizing and grouping related data together.

To declare a struct in Solidity, use the “struct” keyword followed by the struct name and a set of curly braces containing the fields of the struct. For example:

struct User {
string name;
uint age;
}


This code declares a struct called “User” that has two fields: a string called “name” and an unsigned integer called “age”.

## Conclusion

In conclusion, data types are an important concept in Solidity that are used to specify the type of data that a variable or constant can hold. Solidity offers a range of data types, including numeric types, booleans, strings, arrays, mappings, and structs. Understanding the different data types available in Solidity and how to use them is essential for building effective contracts and decentralized applications.

## Exercises

To review these concepts, we will go through a series of exercises designed to test your understanding and apply what you have learned.

#### What is a numeric data type?

A numeric data type is a data type that represents a number. Solidity offers several numeric data types, including integers, fixed-point numbers, and unsigned integers.

#### What is the boolean data type used for in Solidity?

The boolean data type in Solidity is used to represent the values true or false. It is commonly used to represent the results of logical operations or to control the flow of a program.

#### Write a Solidity function that takes in an array of integers as an argument and returns the sum of all the elements in the array.

function findSum(int[] arr) public pure returns (int) {
int sum = 0;
for (uint i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}


#### Write a Solidity function that takes in a struct as an argument and returns the struct’s values as a string.

function convertStructToString(StructType myStruct) public pure returns (string memory) {
return "Name: " + myStruct.name + ", Age: " + myStruct.age + ", Address: " + myStruct.address;
}


#### Write a Solidity function that takes in an array of structs as an argument and returns the sum of all the ages of the structs in the array.

function findSumOfStructAges(StructType[] arr) public pure returns (uint) {
uint sum = 0;
for (uint i = 0; i < arr.length; i++) {
sum += arr[i].age;
}
return sum;
}