Ethereum: How datatypes work in solidity?

How the data types in solidity works: a guide

As we gather in the world of intelligent contracts on the Ethereum blockchain, one of the most fundamental concepts of meaning are the types of data used in the solidarity programming language. In this article, we will explore how data types work in solidity and provide a detailed explanation for their use.

Data types EVM

EVM (Ethereum Virtual Machine) uses a 32 -bit key value to store data. This store is accessed through contracts using the syntax “Contract address”. However, this store is not directly accessible from outside the contract. To interact with the external data, the solidity uses its own types of data.

whole numbers (uint)

In solidity, whole numbers are stored as 32 -bit unpublished numbers (Uint). These whole numbers can hold values ​​between 0 and 2^32 – 1. For example:

`Solidity

Pragma solidity ^0.8.0;

Contract simplestiGeage {

UNT public meter;

Increment () Public Function {

meter ++;

}

}

`

When we call the “increment” function, the contract increases a local variable and updates its value in storage.

strings (string)

String data from solidity is used to store strings. The strings are defined using the keyword String:

Solidity

Pragma solidity ^0.8.0;

Mycontract contract {

String public public message;

Setmesage function (String Memory _message) Public {

message = _message;

}

}

`

When we call for the Setmesage function, it updates a local variable and stores the storage string.

byte (byte)

In solidity, bytes are used to store binary data. Bytes can hold values ​​between 0 and 255 for each of its four elements. For example:

`Solidity

Pragma solidity ^0.8.0;

Mycontract contract {

Bytes Image Public;

Functions Setimage (Bytes Memory _image) Public {

image = _image;

}

}

`

When we call the Setimage function, it updates a local variable and stores the entrance panel in storage.

Address (Address)

In solidity, the addresses are used to represent the contract address. The addresses are represented as 40 -bit hexadecimal strings:

`Solidity

Pragma solidity ^0.8.0;

Mycontract contract {

The address of the public owner;

}

`

The “owner” variable is initialized with a random address.

Data type comparison

| Data type | Use |

| — | — |

| uint | Integer (32 bits) |

| string | String |

| Bytes | Binary data (painting with 4 elements) |

| Address | Own address of the contract

Conclusion

In conclusion, solidity offers several types of built -in data that allow developers to store and handle different types of data in their contracts. Understanding the use of these types of data is essential for the construction of effective and scalable intelligent contracts.

By mastering the use of data types in solidity, you can write more efficient and robust intelligence contracts, which interact with external data in a safe and controlled way.

Ethereum Doesnt Proof

Leave a Reply

Shopping cart

0
image/svg+xml

No products in the cart.

Continue Shopping