Solidity has several built-in data types, including:
Boolean: bool
Integer: int and uint with various bit sizes (int8, uint8, int256, etc.)
Address: address, which represents a 20-byte Ethereum address
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.
Fixed-point numbers: fixed and ufixed with various bit sizes (fixed8x1, ufixed32x18, etc.)
Bytes: bytes and byte with various sizes (bytes1, byte32, etc.)
String: string
Array: array with a fixed or dynamic size (uint[10], string[], etc.)
Struct: struct, which is a custom data type that groups several variables together
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: MITpragmasolidity ^0.8.0;// Enum data typeenumColor { Red, Green, Blue }contract Example {// State variablesuintpublic number;boolpublic isTrue;stringpublic message;addresspublic owner;addresspayablepublic payee;bytes32public data; fixed256x18 public fixedNumber; Color public color;// Struct data typestructPerson {string name;uint age;uint[] favoriteNumbers; }// Array data typeuint[] public numbers;// Mapping data typemapping(address=>uint) public balances;// Constructorconstructor(uint_number,stringmemory_message,address_owner) { number = _number; message = _message; owner = _owner; color = Color.Red; }// Function that uses a structfunctionaddPerson(stringmemory_name,uint_age,uint[] memory_favoriteNumbers) public { Person memory newPerson =Person({name: _name, age: _age, favoriteNumbers: _favoriteNumbers}); }// Function that uses an array and a mappingfunctionaddNumber(uint_number) public { numbers.push(_number); balances[msg.sender] += _number; }}
Data Types Example 2
pragmasolidity ^0.8.0;contract ExampleContract {// State variablesuintpublic myUint;stringpublic myString ="Hello, World!";boolpublic myBool =true;addresspublic myAddress =0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2;addresspayablepublic myPayableAddress =payable(0x5B38Da6a701c568545dCfcB03FcB875f56beddC4);bytes32public myBytes32 ="hello";// EnumenumMyEnum { VALUE1, VALUE2, VALUE3 } MyEnum public myEnum = MyEnum.VALUE1;// StructstructMyStruct {uint id;string name; } MyStruct public myStruct =MyStruct({id:1, name:"John"});// Arraysuint[] public myUintArray;string[] public myStringArray;bool[] public myBoolArray;address[] public myAddressArray;bytes32[] public myBytes32Array; MyEnum[] public myEnumArray; MyStruct[] public myStructArray;// Mappingsmapping(uint=>string) public myUintToStringMapping;mapping(address=>uint) public myAddressToUintMapping;mapping(bytes32=> MyStruct) public myBytes32ToStructMapping;// FunctionsfunctionsetMyUint(uint_myUint) public { myUint = _myUint; }functionaddMyUintToArray(uint_myUint) public { myUintArray.push(_myUint); }functionsetMyString(stringmemory_myString) public { myString = _myString; }functionaddMyStringToArray(stringmemory_myString) public { myStringArray.push(_myString); }functionsetMyBool(bool_myBool) public { myBool = _myBool; }functionaddMyBoolToArray(bool_myBool) public { myBoolArray.push(_myBool); }functionsetMyAddress(address_myAddress) public { myAddress = _myAddress; }functionaddMyAddressToArray(address_myAddress) public { myAddressArray.push(_myAddress); }functionsetMyBytes32(bytes32_myBytes32) public { myBytes32 = _myBytes32; }functionaddMyBytes32ToArray(bytes32_myBytes32) public { myBytes32Array.push(_myBytes32); }functionsetMyEnum(MyEnum_myEnum) public { myEnum = _myEnum; }functionaddMyEnumToArray(MyEnum_myEnum) public { myEnumArray.push(_myEnum); }functionsetMyStruct(uint_id,stringmemory_name) public { myStruct =MyStruct({id: _id, name: _name}); }functionaddMyStructToArray(uint_id,stringmemory_name) public { myStructArray.push(MyStruct({id: _id, name: _name})); }functionsetMyUintToStringMapping(uint_key,stringmemory_value) public { myUintToStringMapping[_key] = _value; }}