DEV Community

Zinzuvadiya Meet
Zinzuvadiya Meet

Posted on

Understanding 4 Different Typesystem In Solidity

The preferred programming language for creating smart contracts on the Ethereum blockchain is Solidity. Solidity uses a strong typesystem because it is a statically-typed language, which enhances the security and effectiveness of your smart contracts by enabling exact data management.

We will examine the four primary sorts of variables in the Solidity typesystem in this blog post: Value Type, Memory Type, User-Defined Type, and Type Aliases. For writing and putting into action smart contracts that communicate with the Ethereum network, it is imperative to have a thorough understanding of these types.

Value Type

You can declare variables in Solidity using a variety of value types. The storage and manipulation of various forms of data in your smart contracts depends on these value types. Solidity's primary value types are as follows:

  1. Boolean (bool): This type represents a binary value, either true or false. It is stored as an 8-bit unsigned number.

  2. Integer (int and uint): Solidity provides different integer types that vary in size and signedness. The int type represents signed integers, while the uint type represents unsigned integers. You can specify the size of integers using the number of bits, ranging from 8 bits (int8/uint8) to 256 bits (int256/uint256).

  3. Address (address and address payable): The address type is used to store Ethereum addresses. It is a 160-bit value and is typically used for representing user accounts or contracts on the Ethereum network. The address payable type is a variant of address that allows the transfer of Ether.

  4. Bytes (bytes): The bytes type is used to store sequences of bytes. It can have a variable length, ranging from 1 to 32 bytes (bytes1 to bytes32). Bytes are stored from left to right, with each byte represented by its hexadecimal value.

contract Example {
    function foo() external {
        bytes4 selector =;
        address caller = msg.sender;
        uint256 callerUint256 = uint256(uint160(caller));
        bool areEqual = ~(int256(0)) == int256(-1);
Enter fullscreen mode Exit fullscreen mode

Memory Type

When handling complex data structures within functions or between function calls, Solidity uses the Memory Type, commonly known as the Link Type or Reference Type. It gives you more freedom for handling data while a contract is being executed by allowing you to interact with dynamically-sized arrays, strings, and structs. Solidity's primary memory types are:

  1. Bytes: The Bytes type allows you to store densely packed raw bytes. It is useful when you want to handle and manipulate byte-level data directly.

  2. String: The String type represents closely packed characters encoded in UTF-8 format. It allows you to work with strings of varying lengths and perform string operations efficiently.

  3. Array of Fixed Length: You can declare arrays with a fixed length using the Memory Type. These arrays store elements of a specific type and have a predetermined number of elements that cannot be changed during execution.

  4. Array of Dynamic Length: The Memory Type also supports arrays with a dynamic length. These arrays can grow or shrink during execution, allowing for flexibility when dealing with data collections of varying sizes.

Let's take a look at an example to understand how Memory Type variables are stored and accessed:

contract Example{
    function foo() external{
        bytes memory someBytes = hex"1122334455";
        string memory someString = "Hello, World";
        uint256[] memory number = new uint256[](10);
        address[2] memory address = [address(1), address(2)];

        // dynamic arrays of static array of strings
        string[2][] memory complexArray = new string[2][](5);       
Enter fullscreen mode Exit fullscreen mode

User-defined Type

Structs are a strong feature in Solidity that gives programmers the ability to construct unique data structures. A struct is a user-defined type that allows the collection of related data into a single unit by allowing the inclusion of many properties or fields. Within Solidity smart contracts, this encourages code organisation, readability, and maintainability.

Structs give you the ability to design unique data structures that are specific to the needs of your smart contract. They enable you to group similar data into useful units, improving the readability and maintainability of your code. In Solidity contracts, structures are especially helpful for describing composite data structures or real-world things.

contract Example {
    struct User {
        string name;
        address account;
        uint32 age;

    function foo() external {
        User memory user = User({ name: "bob", account: msg.sender, age: 32 });
Enter fullscreen mode Exit fullscreen mode

Type Aliases

Type aliases serve as aliases for existing types in Solidity, providing a way to create alternative names for types or to create more descriptive names for complex types. They contribute to code clarity and help in conveying the purpose and intention of variables or functions. Read More

Here's an example of aliases type in Solidity:

type myUint is uint256;

library MyUinter{
    function add(myUint a, myUint b) internal pure returns(myUint) {
        return myUint.wrap( myUint.unwrap(a) + myUint.unwrap(b) );
Enter fullscreen mode Exit fullscreen mode

👋🏻 In this blog post, we have explored the Solidity type system, which plays a vital role in ensuring the security and effectiveness of smart contracts on the Ethereum blockchain. We discussed the four main types of variables: value types, memory types, user-defined types (such as structs), and type aliases. Next, we will dive into the various storage locations for Solidity variables. These locations determine where variables are stored and accessed on the blockchain.

Until Next Time

Top comments (0)