Data Types in Solidity smart contracts

Data types in Solidity represents the type of data the program is to use. The type can be numeric, alphanumeric, decimal, bool, etc. These attributes tell the program compiler how the programmer intends to use the data by constraining the values that a variable or a function might use. Below are common data types that are used in different programming languages.

Examples of data types in programming languages

Data Type Example Values
Intiger123456789
StringHello World
BooleanTrue

Data types in Solidity

Solidity is a statically typed language which means the type of each variable needs to be specified throughout your code. Data types instruct the compiler to check the usage of the variables in your contract. Declared data types have default values referred to as zero state (0). For example a boolean’s default value is False and a uint’s default value is 0. The concept of undefined or null values does not exist in solidity. So a data type will have some value (for example – hi, 123, true, etc.) or 0 as a default value. For more information read the docs.

The Solidity programming language supports different data types. Examples of these types can be found in the grid below.

Data Type Name used in code Example Value Bytes
Boolean boolTrue or False1
StringstringHello32
Integers – signed and unsigned integers of various sizes. Specify sizes in steps of 8 from 8 to 256. For example uint8, uint16, uint256. uint and int are aliases for uint256 and int256int

uint
1234532 but can make is smaller by specifying.
uint128 – 16 bytes
uint96 – 12 bytes
uint64 – 8 bytes
Address – Address payable includes the members transfer and send so it can receive Etheraddress

address payable
0x1f9840a85d5aF5bf1D…20
Fixed Point Number (signed and unsigned)fixed

ufixed
25.50 (fixed number of decimal places)
Byte – A single byte. You can also define arrays of 1–32 bytes using the type bytes2, byte3, up to bytes32bytebyte singleChar = ‘h’

bytes16 msgHello = ‘Hello, world!’;
1 but can make larger by specifying

Note: Byte information comes in handy when you want to optimize storage in your contract. Only use what you need because storage on the blockchain is expensive.

Operators for Booleans

In your smart contact you can declare a bool which contains a true or false values. Then use the operators below to evaluate your data in different expressions.

Operator Name Denotation
!Negative
&&Conjunction, “and”. Short circuit – If part of the expression evaluates to true the remaining part of the expression will not be evaluated
||Disjunction, “or”. Short circuit – If part of the expression evaluates to true the remaining part of the expression will not be evaluated
==Equality
!=Inequality

For a more comprehensive list of how operators can be used with each data type you can read the docs.

Operators for Integers

In your smart contact you can declare various integers then use the operators below to evaluate your data in different expressions.

Operator Name Denotation
<Less then. Evaluates to true or false
<= Less then or equal to. Evaluates to true or false
>Greater then. Evaluates to true or false
>=Greater then or equal to. Evaluates to true or false
==Equals. Evaluates to true or false
!=Not equal. Evaluates to true or false
+Addition
Subtraction
*Multiplication
/Division
%Modulo – the remainder after dividing one number by another
**Exponentiation
++Increase the integer value by 1
Decrease the integer value by 1

For a more comprehensive list of how operators can be used with each data type you can read the docs.

Operators for Fixed Point Numbers

In your smart contact you can declare various fixed point numbers then use the operators below to evaluate your data in different expressions.

Operator Name Denotation
<Less then. Evaluates to true or false
<= Less then or equal to. Evaluates to true or false
>Greater then. Evaluates to true or false
>=Greater then or equal to. Evaluates to true or false
==Equals. Evaluates to true or false
!=Not equal. Evaluates to true or false
+Addition
Subtraction
*Multiplication
/Division
%Modulo – the remainder after dividing one number by another

For a more comprehensive list of how operators can be used with each data type you can read the docs.

Operators for Addresses

Use the operators below to evaluate address data in different expressions. The operators below can be used with this type.

Operator Name Denotation
<Less then. Evaluates to true or false
<= Less then or equal to. Evaluates to true or false
>Greater then. Evaluates to true or false
>=Greater then or equal to. Evaluates to true or false
==Equals. Evaluates to true or false
!=Not equal. Evaluates to true or false

Note: You might not need to care about the distinction between address and address payable. You can only use address in your code. For example, if you are using the withdrawal pattern, you can (and should) store the address itself as address, because you invoke the transfer function on msg.sender, which is already set to address payable.

Where are data types used within a contract

Data types are declared and used throughout your smart contract. Some example where you will declare a data type are:

  • State Variables – In the beginning of your contract to declare data you want to save to the block chain
  • Functions – Data to use within a function

The sample contract below declares several different data types. Read the comments in the code to better understand the examples.

pragma solidity ^0.6.0;

//different data types

contract MyContract {

    //state variables at the beginning of the contract are declared and stored on the blockchain
    //each variable needs to have a data type

    //data type string called myString
    string public myString = "hello";

    //data type bytes32 is encoded and used for performance
    bytes32 public myBytes32 = "hello world";
    
    //data type int called myInt
    int public myInt = 1;

    //data types uint called myUint
    uint public myUint = 2;
    
    //data types uint256 called myUint256
    uint256 public myUint256 = 3;
    uint8 public myUint8 = 4;

    //data type address called myAddress
    address public myAddress = 0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984;


    //function with local variable are stored in memory.  
    //variables within functions have a declared data type
    function getValue() public pure returns(uint) {
        uint value = 6;
        return value;
    }

Try it in Remix

Assignment Operators

These operators are for the assignment of value to a variable. The operand at the left side is variable while operand at the right side is value. Solidity supports the following arithmetic operators.

Operator Name Denotation
=Simply assigns the value at the right side to the operand at the left side
+=Adds operand at the right side to operand at the left side and assigns the value to left operand
-=Subtracts operand at the right side from operand at the left side and assigns the value to left operand
*=Multiplies both the operands and assigns the value to the left operand
/=Divides operand at the left side by operand at the right side and assigns the value to left operand
%=Divides operand at the left side by operand at the right side and assign the remainder to left operand

pragma solidity ^0.8.7;

// Creating a contract to demonstrate how assignment operators work
contract SolidityOperatorExample {
    
    // variables are assigned a value
    // function preforms operation and changes values of variables
    // after deployment execute getResults function
    // review values of each variable

		// Declaring variables
		uint public variable_add = 70;
		uint public variable_sub = 50;
		uint public variable_mul = 25;
		uint public variable_div = 80;
		uint public variable_mod = 68;
	
		// function that demonstrates how assignment operators work
		function getRusults() public{
		variable_add += 25;
		variable_sub -= 30;
		variable_mul *= 25;
		variable_div /= 5;
		variable_mod %= 60;
		return ;
		}
}

Try it in Remix

This code is for learning and entertainment purposes only. The code has not been audited and use at your own risk. Remember smart contracts are experimental and could contain bugs.

Click here for more information about how to use the Ethereum test network and how to obtain test ETH.

Next Review – Constructors in Solidity smart contracts

1 thought on “Data Types in Solidity smart contracts

Leave a Reply