Introduction to various types
- Numeric type (Value Type) : including Boolean type (bool), integer type (int, uint, uint256), etc. This type of variable directly passes the value when assigning value.
- Reference Type : Including arrays and structures. These types of variables take up a lot of space, and the address (similar to a pointer) is passed directly during assignment.
- Mapping Type :
Solidity
Hash table in. - Function Type :
Solidity
In the documentation, functions are classified as numeric types, but I think they are very different from other types, so they are classified into a separate category.
boolean
Boolean type is a binary variable with a value of true or false. In vernacular, it can only be right or wrong.
Boolean operators, && and || operators follow short-circuiting rules, including:
-
! (logical NOT)
-
&& (logical AND, "and") If both parties have a false result, the result is false.
-
|| (Logical OR, "or") If both parties have a true result, it is true. When the first one is true, the second one is not executed.
-
== (equal) determines whether the values are equal
-
!= (not equal to)
integer
// 整型
int public _int = -1; // 整数,包括负数
uint public _uint = 1; // 正整数
uint256 public _number = 20220330; // 256位正整数
Commonly used integer operators include:
code example
Address type (how to use it will be explained in the function article)
The address type stores a 20-byte value (the size of an Ethereum address). The address type also has member variables and serves as the basis for all contracts. There are ordinary addresses and addresses that can transfer ETH (payable). Among them, the address modified by payable has two more members, transfer and send, than the ordinary address. In the payable-modified address, failure to execute send will not affect the execution of the current contract (but a false value will be returned, and developers need to check the send return value). balance and transfer() can be used to query ETH balances and secure transfers (built-in handling of execution failures).
code
// 地址
address public _address = 0x7A58c0Be72BE218B41C608b7Fe7C5bB630736C71;
address payable public _address1 = payable(_address); // payable address,可以转账、查余额
// 地址类型的成员
uint256 public balance = _address1.balance; // balance of address
Fixed length byte array
There are two types of byte arrays, one with fixed length (byte, bytes8, bytes32) and the other with variable length. Fixed-length ones belong to numeric type,
The fixed-length bytes of reference types can store some data and consume less gas.
code
// 固定长度的字节数组
bytes32 public _byte32 = "HelloWorld";
bytes1 public _byte = _byte32[0]; //存储_byte32的第一个字节,为0x48。
bytes1 stores a byte.
The HelloWorld variable is stored in the variable _byte32 in bytes. When converted into hexadecimal, it is: 0x48656c6c6f576f726c6400000000000000000000000000000000000000000000
enum (basically no one uses it)
Enumeration (enum) is a user-defined data type in Solidity. It is mainly used to assign names to uints, making programs easier to read and maintain. It is similar to enum in C language, using a name instead of uint starting from 0:
it can be clearly seen that if it is not explicitly converted to uint and vice versa, the result will not appear.