Kolapo Damola Usman
Posted on August 28, 2022
Solidity is a smart contract programming language. It is most commonly used to write smart contracts for the Ethereum blockchain but can be used to implement smart contracts on any blockchain that uses the Ethereum Virtual Machine (EVM).
Like all other programming languages, datatypes in Solidity can be classified into 2 types, Values and References.
-
Values: values simply mean that the datatype stores a value e.g boolean stores either
true
orfalse
. - References: datatypes of type reference do not store a value instead they store a reference to where the actual data is stored e.g an Array is a datatype of type reference.
In this article, we'll be looking at Value Datatypes and how to use them in a solidity contract.
Boolean
contract ValueType {
bool public boo= true;
}
Here we have a contract named ValueType
, we will declare all the value datatypes inside this contract.
In the contract, we declare a boolean datatype using the keyword bool
, this boolean is declared as public
so it can be called outside the contract.
The variable is given the name boo
and its value set to true
, the value of a boolean can either be true
or false
.
Unsigned Integer
Next up, we'll be looking at the unsigned integer datatype.
An unsigned integer means the datatype has to be greater than or equal to zero, we can not use negative numbers with unsigned integers only zero or positive numbers.
contract ValueType {
uint public numU= 123;
}
Here uint
keyword is used to declare an unsigned integer, the variable is declared as public
, the variable is given the name numU
, and its value is set to 123
.
The keyword uint
is an alias for 0
to 2^256 - 1
There are different variants of uint
with different ranges starting from uint8
, uint16
, etc which goes all the way to uint256
.
Among these variations of uint
, the most common one we will come across often is uint256
.
Unsigned integers only support numbers that are greater than zero, but what of cases where we need to use a negative number, in this case, we use integers.
Integers
Next up, let's take a look at integers.
Integers allow us to use positive and negative numbers in our contract.
contract ValueType {
int public numI= -123;
}
An integer is declared using the int
keyword, the variable here is declared to be public
, the variable is given the name numI
and the value set to -123
.
Similar to unsigned integers, the keyword int
is also an alias for int256
.
There are also different variations of int
, starting from int8
, int16
to int256
.
Address
Another value datatype that we will come across a lot in Solidity is type address
.
contract ValueType {
address public addr= 0xFAed2F163D65141FbD48fD5FE1a4C08c2e50a4bF;
}
An address datatype is declared using the keyword address
, this variable is made public
, and the address is given the name addr
and we set the value to a random Ethereum wallet address.
byte32
The last value datatype we will be looking at in this article is byte32
, we will encounter this datatype when working with the cryptographic cache function available in solidity called kachek256
.
contract ValueType {
bytes32 public b32= 0x0000000000000000000000000000000;
}
We declare a bytes32
by using the keyword bytes32
, the variable is made public, and given the name b32
, the value here is set to some random 32bytes.
Conclusion
We haven't covered all the Valuetypes available in Solidity, in this article, but these are the most common ones we will come across when writing a Smart contract, you can find a full list of Solidity Valuetypes in the documentation.
Thanks for reading, If you enjoyed reading this as much as I enjoyed writing it, then Like and Share this with your friends and feel free to connect with me on Twitter 👨💻.
Posted on August 28, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.