The first step in getting started with Solidity: data types

Introduction to various types

  1. 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.
  2. 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.
  3. Mapping Type : SolidityHash table in.
  4. Function Type : SolidityIn 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.

Insert image description here
Insert image description here
Boolean operators, && and || operators follow short-circuiting rules, including:

  1. ! (logical NOT)
    Insert image description here
    Insert image description here

  2. && (logical AND, "and") If both parties have a false result, the result is false.
    Insert image description here
    Insert image description here

  3. || (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.
    Insert image description here
    Insert image description here

  4. == (equal) determines whether the values ​​are equal
    Insert image description here
    Insert image description here

  5. != (not equal to)
    Insert image description here
    Insert image description here

integer

   // 整型
    int public _int = -1; // 整数,包括负数
    uint public _uint = 1; // 正整数
    uint256 public _number = 20220330; // 256位正整数

Insert image description here
Insert image description here

Commonly used integer operators include:

Insert image description here

code example

Insert image description here
Insert image description here

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

Insert image description here
Insert image description here

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.
Insert image description hereThe 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:
Insert image description here
Insert image description here
it can be clearly seen that if it is not explicitly converted to uint and vice versa, the result will not appear.

Guess you like

Origin blog.csdn.net/Wtzink/article/details/134231941