header-langage
简体中文
繁體中文
English
Tiếng Việt
한국어
日本語
ภาษาไทย
Türkçe
Scan to Download the APP

A minimalist introduction to Solidity | Lecture 2: Numerical types

Read this article in 9 Minutes
This article analyzes the 4 variable types in Solidity, and introduces in detail the Boolean type, integer type, address, fixed-length byte array and enumeration in the value type.
Original title: " A minimalist introduction to Solidity: 2. Numerical types "

Original source: 0xAA  


I am re-learning solidity recently, to consolidate the details, and also write a "Solidity minimalist introduction" for beginners to use (programmers can find another tutorial), updated 1 week -3 speaking.


All codes are open source on github: github.com/AmazingAng/WTFSolidity


Solidity Variable type Value type (Value Type): Including Boolean type, integer type, etc., when this type of variable is assigned, the value is directly passed. Reference type (Reference Type): Including arrays and structures, such variables take up a lot of space, and the address is directly passed when assigning a value (similar to a pointer). Mapping Type: Hash table in Solidity. Function Type (Function Type): In the Solidity 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.


We only introduce some commonly used types, not commonly used ones. This article covers numeric types, Lecture 3 covers function types, and Lecture 4 covers references and mappings.


Numerical type 1. Boolean


Boolean is a binary variable, the value is true or false.



Boolean Operators, including:


! (logical NOT)

&& (logical AND, "and")

||(logical or, "or")

==(equal)

!=(not equal)


< /p>

Code:



< /p>

In the above code: the value of the variable _bool is true; _bool1 is the negation of _bool, which is false; _bool && _bool1 is false; _bool || _bool1 is true; _bool == _bool1 is false; _bool != _bool1 is true.


2. Integer type


Integer type is an integer in solidity, the most commonly used ones include



Commonly used Integer operators include:


Comparison operators (returning a Boolean value): <=, <, ==,  !=, >=, >

Arithmetic operators: +, -, unary operation -, +, *, /,  % (remainder), ** (power)


Code:



You can run the code to see what the 4 variables are. A POAP for a correct answer?


3. Address type


The address type (address) stores a 20-byte value (the size of an Ethereum address). Address types also have member variables and serve as the basis for all contracts. There are ordinary addresses and addresses where ETH can be transferred (payable). The payable address has two members, balance and tranfer(), which are convenient for querying ETH balance and transfer.


Code



When we introduce functions in the next lecture, we will introduce how to use the address type.


4. Fixed-length byte array


There are two types of byte array bytes, one One is of fixed length (byte, bytes8, bytes32), and the other is of variable length. The fixed length is a numeric type, and the variable length is a reference type (to be discussed later).

Fixed-length bytes can store some data and consume less gas.


Code:



  It can be explicitly converted to and from uint, and will check whether the converted positive integer is within the length of the enumeration Inside, otherwise an error will be reported:



A relatively unpopular variable of enum, almost no one uses it.


Summary


< p>

In the second lecture, we introduced 4 variable types in solidity, and introduced in detail the Boolean type, integer type, address, and fixed length in the value type. Byte arrays and enumerations. We will introduce several other types later.


Original link


Welcome to join the official BlockBeats community:

Telegram Subscription Group: https://t.me/theblockbeats

Telegram Discussion Group: https://t.me/BlockBeats_App

Official Twitter Account: https://twitter.com/BlockBeatsAsia

举报 Correction/Report
Choose Library
Add Library
Cancel
Finish
Add Library
Visible to myself only
Public
Save
Correction/Report
Submit