Open In App

Solidity – Types

Solidity is a statically typed language, which implies that the type of each of the variables should be specified. Data types allow the compiler to check the correct usage of the variables. The declared types have some default values called Zero-State, for example for bool the default value is False. Likewise other statically typed languages Solidity has Value types and Reference types which are defined below:

Value Types

Value-type variables store their own data. These are the basic data types provided by solidity. These types of variables are always passed by value. The variables are copied wherever they are used in function arguments or assignments. Value type data types in solidity are listed below: 



Example: 

In the below example, the contract Types initialize the values of different types of Values Types.






// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.22 <0.9.0; 
/// @title A contract for demonstrate Value types
/// @author Jitendra Kumar
/// @notice For now, this contract just show how Value types works in solidity
contract Types {
 
    // Initializing Bool variable
    bool public boolean = false;
     
    // Initializing Integer variable
    int32 public int_var = -60313;
 
    // Initializing String variable
    string public str = "GeeksforGeeks";
 
    // Initializing Byte variable
    bytes1 public b = "a";
     
    // Defining an enumerator
    enum my_enum { geeks_, _for, _geeks }
 
    // Defining a function to return
    // values stored in an enumerator
    function Enum() public pure returns(
    my_enum) {
        return my_enum._geeks;
    }
}

Output:

Reference Types

Reference type variables store the location of the data. They don’t share the data directly. With the help of reference type, two different variables can refer to the same location where any change in one variable can affect the other one. Reference types in solidity are listed below: 

Example: In the below example, the contract Types initialize the values of various Reference Types.




// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.22 <0.9.0; 
/// @title A contract for demonstrate Reference types
/// @author Jitendra Kumar
/// @notice For now, this contract just show how Reference types works in solidity
contract mapping_example {
 
    // Defining an array
    uint[5] public array
    = [uint(1), 2, 3, 4, 5] ;
     
    // Defining a Structure
    struct student {
        string name;
        string subject;
        uint8 marks;
    }
 
    // Creating a structure object
    student public std1;
 
    // Defining a function to return
    // values of the elements of the structure
    function structure() public returns(
    string memory, string memory, uint){
        std1.name = "John";
        std1.subject = "Chemistry";
        std1.marks = 88;
        return (
        std1.name, std1.subject, std1.marks);
    }
     
    // Creating a mapping
    mapping (address => student) result;
    address[] student_result;
}

 Output:


Article Tags :