Hello geeksters!
So what is Solidity?
Solidity is the language used by web3 developers to write smart contracts (they are indeed very smart) on EVM (Ethereum Virtual Machine) and other EVM compatible machines. Basically its like a special programming language for smart contract development. Now, it is important to know what data types are supported in Solidity. Its pretty similar to most programming languages where you have a type to store a number, strings, arrays, etc...
Let's do a deep dive into the very interesting world of different data types in Solidity.
On a high level, there are two distinct data types.
- Value Types - These are stored directly on the memory. So if they are assigned, they're copied. These are further subdivided into
-
uint
- Unsigned integers.
uint x = 100;
uint can be of different types as well. uint, uint8, uint32, etc. These can be used on various situations based on requirement to save gas. But defining a variable under uint works as well.
-
int
- Signed integers. Basically you can store negative integers in this data type when compared to uint.
int x = 100;
int can also be of different types similar to uint as int, int8, int32, etc.
-
bool
- Stores a true or false value.
bool isPayable = True;
-
address
- Stores a 20 byte Ethereum address. It can be the address of the wallet from which you access the smart contract.
address owner = msg.sender;
- Reference Types - These data types are stored by reference, that is the variable defined by these data types store the memory address of where the actual data is located, rather than the data itself. These are of various types such as:
-
string
- It stores a string of characters like names, places, etc.
string name = "Achu";
-
array
- It can be used to store a list of items of similar type. For example, a list of names of students in a class or a list of marks obtained in a class.
uint[] public scores; //stores a list of integers in array called scores
-
mapping
- Creates a key-value hash table. Imagine a dictionary containing a name and an associated key. Like a student's name and their total marks.
mapping(address => uint) balances; //maps the addresses to integer values in a table called balances
-
struct
- Similar to array but the difference is that it can store items of different types in it instead of the same type like in arrays. Like a student's name, address and score in one data structure.
struct Student {
string name;
string address;
uint scores;
}
Mastering data types in Solidity is the foundation for smart contract development. Whether you're building DeFi, NFTs, or DAOs, using the right types can:
- Prevent bugs
- Reduce gas costs
- Improve security
Top comments (0)