Recently, I have been revisiting Solidity, consolidating the finer details, and writing "WTF Solidity" tutorials for newbies.
Twitter: @0xAA_Science | @WTFAcademy_
Community: Discord|Wechat|Website wtf.academy
Codes and tutorials are open source on GitHub: github.com/AmazingAng/WTF-Solidity
Variable Types
-
Value Type:This includes boolean, integer, etc. These variables directly pass values when assigned.
-
Reference Type:including arrays and structures. These variables take up more space, directly pass addresses (similar to pointers) when assigned, and can be modified with multiple variable names.
-
Mapping Type: hash tables in Solidity.
-
Function Type:The Solidity documentation classifies functions into value types. But it's very different from other types, and I put it in a separate category.
Only the commonly used types will be introduced here. In this chapter, we will introduce value types.
Value types
1. Boolean
Boolean is a binary variable, and its values are true
or false
.
Operators for Boolean type include:
!
(logical NOT)&&
(logical AND)||
(logical OR)==
(equality)!=
(inequality)
Code:
From the above source code: the value of the variable _bool
is true
; _bool1
is not_bool
, which yields false
; _bool && _bool1
is false
;_bool || _bool1
is true
;_bool == _bool1
is false
;and _bool != _bool1
is true
.
Important note: The &&
and ||
operator follows a short-circuit evaluation rule. This means that for an expression such as f(x) || g(y)
, if f(x)
is true
, g(y)
will not be evaluated.
2. Integers
Integers types in Solidity include signed integer int
and unsigned integer uint
. It can store up to a 256-bit integers or data units.
Commonly used integer operators include:
- Inequality operator (which returns a Boolean):
<=
,<
,==
,!=
,>=
,>
- Arithmetic operator:
+
,-
,*
,/
,%
(modulo),**
(exponent)
Code:
You can run the above code and check the values of each variable.
3. Addresses
Addresses have the following 2 types:
-
address
: Holds a 20 byte value (size of an Ethereum address). -
address payable
: Same asaddress
, but with the additional memberstransfer
andsend
to allow ETH transfers.
Code:
4. Fixed-size byte arrays
Byte arrays in Solidity come in two types:
- Fixed-length byte arrays: belong to value types, including
byte
,bytes8
,bytes32
, etc, depending on the size of each element (maximum 32 bytes). The length of the array can not be modified after declaration. - Variable-length byte arrays: belong to reference type, including
bytes
, etc. The length of the array can be modified after declaration. We will learn more detail in later chapters
Code:
In the above code, we assigned the value MiniSolidity
to the variable _byte32
, or in hexadecimal: 0x4d696e69536f6c69646974790000000000000000000000000000000000000000
And _byte
takes the value of the first byte of _byte32
, which is 0x4d
.
5. Enumeration
Enumeration (enum
) is a user-defined data type within Solidity. It is mainly used to assign names to uint
, which keeps the program easy to read.
Code:
It can be converted to uint
easily:
enum
is a less popular type in Solidity.
Demo in Remix
-
After deploying the contract, you can check the values of each variable:
-
Conversion between enum and uint:
Summary
In this chapter, we introduced the variable types in Solidity, they are value type, reference type, mapping type, and function type. Then we introduced commonly used types: boolean, integer, address, fixed-length byte array, and enumeration in value types. We will cover other types in the subsequent tutorials.