Value Data Types

Learn about value, the first set of data types.

Data types in Solidity

As we’ve learned, Solidity is a statically typed programming language. This means that we have to provide the type of data we want a variable to hold when defining it and we cannot assign or reassign a value of a different type to this variable. This makes it vital to understand the different data types and what we can store in them.

Solidity uses two main types: value and reference. Reference variables store references to their data, while value variables store their data directly.

We’ll be making references to and modifying our existing contract code to exemplify these data types. In this code, the variables are defined but not assigned any values. When we attempt to retrieve variables defined like this, we receive the default value associated with the data type, as shown in the code widget below:

Press + to interact
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/**
* @title Contact
* @dev Store & retrieve contacts of friends and family
*/
contract Contact {
address owner;
uint256 public number;
string name;
constructor() {
owner = msg.sender;
}
/**
* @dev Save new contact
* @param number_ number of contact to store
* @param name_ name of contact to store
*/
function save(uint256 number_, string calldata name_) public {
number = number_;
name = name_;
}
}

We can test and execute the above code using the widget below:

Value type data

A value type variable stores data directly within its own memory allocation. Variables of this type are always passed by value. In ...