Data Types

Solidity has several built-in data types, including:
  1. 1.
    Boolean: bool
  2. 2.
    Integer: int and uint with various bit sizes (int8, uint8, int256, etc.)
  3. 3.
    Address: address, which represents a 20-byte Ethereum address
  4. 4.
    Address payable: Defines the owner variable as an address that can receive ether. The msg.sender address is cast to this type in the constructor.
  5. 5.
    Fixed-point numbers: fixed and ufixed with various bit sizes (fixed8x1, ufixed32x18, etc.)
  6. 6.
    Bytes: bytes and byte with various sizes (bytes1, byte32, etc.)
  7. 7.
    String: string
  8. 8.
    Array: array with a fixed or dynamic size (uint[10], string[], etc.)
  9. 9.
    Struct: struct, which is a custom data type that groups several variables together
  10. 10.
    Mapping: mapping, which is a dynamic array-like data structure that maps keys to values

Data Types Example

// SPDX-License-Identifier: MIT pragma solidity ^0.8.0;
// Enum data type enum Color { Red, Green, Blue }
contract Example { // State variables uint public number; bool public isTrue; string public message; address public owner; address payable public payee; bytes32 public data; fixed256x18 public fixedNumber; Color public color;
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// Enum data type
enum Color { Red, Green, Blue }
contract Example {
// State variables
uint public number;
bool public isTrue;
string public message;
address public owner;
address payable public payee;
bytes32 public data;
fixed256x18 public fixedNumber;
Color public color;
// Struct data type
struct Person {
string name;
uint age;
uint[] favoriteNumbers;
}
// Array data type
uint[] public numbers;
// Mapping data type
mapping(address => uint) public balances;
// Constructor
constructor(uint _number, string memory _message, address _owner) {
number = _number;
message = _message;
owner = _owner;
color = Color.Red;
}
// Function that uses a struct
function addPerson(string memory _name, uint _age, uint[] memory _favoriteNumbers) public {
Person memory newPerson = Person({name: _name, age: _age, favoriteNumbers: _favoriteNumbers});
}
// Function that uses an array and a mapping
function addNumber(uint _number) public {
numbers.push(_number);
balances[msg.sender] += _number;
}
}

Data Types Example 2

pragma solidity ^0.8.0;
contract ExampleContract {
// State variables
uint public myUint;
string public myString = "Hello, World!";
bool public myBool = true;
address public myAddress = 0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2;
address payable public myPayableAddress = payable(0x5B38Da6a701c568545dCfcB03FcB875f56beddC4);
bytes32 public myBytes32 = "hello";
// Enum
enum MyEnum {
VALUE1,
VALUE2,
VALUE3
}
MyEnum public myEnum = MyEnum.VALUE1;
// Struct
struct MyStruct {
uint id;
string name;
}
MyStruct public myStruct = MyStruct({id: 1, name: "John"});
// Arrays
uint[] public myUintArray;
string[] public myStringArray;
bool[] public myBoolArray;
address[] public myAddressArray;
bytes32[] public myBytes32Array;
MyEnum[] public myEnumArray;
MyStruct[] public myStructArray;
// Mappings
mapping(uint => string) public myUintToStringMapping;
mapping(address => uint) public myAddressToUintMapping;
mapping(bytes32 => MyStruct) public myBytes32ToStructMapping;
// Functions
function setMyUint(uint _myUint) public {
myUint = _myUint;
}
function addMyUintToArray(uint _myUint) public {
myUintArray.push(_myUint);
}
function setMyString(string memory _myString) public {
myString = _myString;
}
function addMyStringToArray(string memory _myString) public {
myStringArray.push(_myString);
}
function setMyBool(bool _myBool) public {
myBool = _myBool;
}
function addMyBoolToArray(bool _myBool) public {
myBoolArray.push(_myBool);
}
function setMyAddress(address _myAddress) public {
myAddress = _myAddress;
}
function addMyAddressToArray(address _myAddress) public {
myAddressArray.push(_myAddress);
}
function setMyBytes32(bytes32 _myBytes32) public {
myBytes32 = _myBytes32;
}
function addMyBytes32ToArray(bytes32 _myBytes32) public {
myBytes32Array.push(_myBytes32);
}
function setMyEnum(MyEnum _myEnum) public {
myEnum = _myEnum;
}
function addMyEnumToArray(MyEnum _myEnum) public {
myEnumArray.push(_myEnum);
}
function setMyStruct(uint _id, string memory _name) public {
myStruct = MyStruct({id: _id, name: _name});
}
function addMyStructToArray(uint _id, string memory _name) public {
myStructArray.push(MyStruct({id: _id, name: _name}));
}
function setMyUintToStringMapping(uint _key, string memory _value) public {
myUintToStringMapping[_key] = _value;
}
}