Data types in Solidity

Data types in Solidity

Solidity is an object-oriented programming language that is used to create smart contracts for Ethereum . In this article we are going to explain the basic datatypes in Solidity. In Solidity, data types for each variable must be specified at compile time, and variables can only accept variables whose data types were initially declared.

There are two main type of Datatypes available in Solidity,

  1. Value Type
  2. Reference Type

Value Types

Variables of a value type store their own data. These are the fundamental data types that Solidity offers. Always passed by value, these variables are. Anywhere the variables are utilized as function parameters or assignment, a copy is made of them. The below is a list of value type data types in solidity.

  • Signed integers
  • Unsigned integers
  • Boolean
  • Addresses
  • Enums
  • Bytes

Signed Integers

signed integer We are declaring using the 'int' keyword . and also we can declare 'int256' , 'int8', 'int16', and 'int32' . Based on need ,we can use int8 or int16 or int32 so on. int256 range of 2 255 to 2 255 - 1

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Contract {
    int number1 = 234555;
    int8 number2 = 2;
    int16 number3 = 234;
    int256 number4 = 2349000666554;
}

Unsigned integers

unsigned integers declared with 'uint' keyword. non-negative values store and range will be 0 to 2 ** 256 - 1. similar to signed integer we can declared like 'uint8', 'uint16' , 'uint32' .

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Contract {
    uint public number1 = 234555;
    uint8 public number2 = 2;
    uint16 public number3 = 234;
    uint256 public number4 = 2349000666554;
}

Boolean

In Here we use 'bool' keyword for declare Boolean datatypes. Boolean datatype only have 2 values . 'true' or 'false' . Boolean default value is 'false' .

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Contract {

    bool public show;
    bool public hide = true;
    bool public isVerified = false;
}

Addresses

This datatype is specifically used to hold the 160 bit size of an Ethereum address. For declaring the Address datatype, we can use 'address' and 'address payable' keywords.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Contract {

    address public  myAddress = 0x6aF1a449f540F30650D2eD67702543c15B10291E;
}

Enums

Enums in Solidity stand for Enumerable. Enums are user-defined data types that limit a variable's value to one of the predefined values. for declare use 'enum' keyword.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Contract {

    // creating an enum
    enum Switch { ON, OFF }

    // declaring a veriable of type enum
    Switch switch1;

    //function to turn on the switch
    function switchOn() public {
        // set the value of switch to ON
        switch1 = Switch.ON;
    }

    // function to turn off the switch
    function switchOff() public {
        switch1 = Switch.OFF;
    }

    //function to get the value of the button 
    function getSwitchState() public view returns (Switch) {
        //return the value of button 
        return switch1;
    } 


}

Bytes

In Solidity, a byte refers to an 8-bit signed integers . used 'bytes' keyword for declared

Reference types

Solidity reference types differ from value types in that they do not directly store values. Instead, reference types store (or "reference") the address of the data's location rather than sharing the data directly. Because they deal with storage locations, reference data types must be handled with caution. The location of data determines the amount of gas used in a transaction, which can have a negative impact on smart contract development performance. Fixed-sized arrays, dynamic-sized arrays, array members, byte arrays, string arrays, structs, and mapping are all reference data types in Solidity.

Arrays

Arrays are collections of variables of the same data type, each with its own unique index. The size of an array can be fixed or dynamic.

Fixed-size arrays

When fixed-size arrays are declared, they have a predefined size.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Contract {

    uint[10] balance;

}

Dynamic Array

When the array is declared, its size is not specified. The size of the array changes as elements are added, and the size of the array is determined at runtime.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Contract {

    int[] data;

}

Structs

In Solidity, the struct reference type refers to a new (or custom) data type. The 'struct' keyword can be used to define a structure made up of multiple variables that can be both value type and reference type.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Contract {

    struct student {
        string name;
        string age;
        uint16 gender;
    }

}

Mapping

Mapping in Solidity works similarly to a hashtable or dictionary in other programming languages. Mapping is the most common reference type in Solidity. Mapping types are used to store data as key-value pairs, where the key can be any of the built-in data types (except reference types) and the value can be any type.

mapping(key => value) <access specifier> <name>;
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Contract {

    struct student
    {
        string name;
        string subject;
        uint8 marks;

    }

    mapping (
    address => student) result;
    address[] public student_result;

}

Conclusion

We discussed the relationship and application of solidity value types and reference types in smart contract development. I hope it will help you for web3 learning journey . Thank you