Solidity is an object-oriented, high-level language for creating smart contracts.
Smart contracts are programs that decide the behaviour of accounts within the Ethereum state. Solidity is a statically typed language. It supports inheritance, libraries and complex user-defined types among other features. With Solidity, you can create contracts for uses such as voting, crowdfunding, blind auctions, and multi-signature wallets. There are various data types in solidity and Value Type is one of them. In this blog, we will be seeing some of the value types in solidity.
Types in Solidity
Solidity is a statically typed language, which means that we need to specify the type of each variable. Solidity provides several elementary types and we can combine them to form complex types.
One of the Types is Value Type. So, let us see what are the value types.
The following types are also called value types because variables of these types will always be passed by value, i.e. they are always copied when we use them as function arguments or in assignments.
Firstly, we will talk about Boolean type. Boolean values are represented by
bool. The value of the bool type can be true or false. The operators applicable on bool type are –
- ! – logical negation
- && – logical and
- || – logical or
- equality – ==
- inequality – !=
The logical “and” and logical “or” operator follow the same short-circuiting rules as in other programming languages. For example, In the expression
op1(x) || op2(y), if
op1(x) evaluates to true,
op2(y) will not be evaluated.
The integer type is denoted by keywords uint8 to uint256 and int8 to int256 for
signed integers respectively.
int are aliases for
The operations which we can perform on integers are –
>(result value is
- Bit operators:
^(bitwise exclusive or),
- Shift operators:
- Arithmetic operators:
-(only for signed integers),
Fixed Point Numbers
Fixed point numbers are similar to float data type but the main difference is that we need to specify the number of bits required for the integer part and fractional part in fixed-point numbers whereas, in float, it is not necessary.
We use fixed/ufixed keywords to specify fixed-point numbers of various sizes. We need to specify the fixed-point numbers in the following manner –
M represents the number of bits taken by the type and
N represents how many decimal points are available.
Solidity does not fully support Fixed Point numbers yet, hence we can declare Fixed Point numbers but we cannot assign them to or from them.
The operations available for fixed-point numbers are –
- Arithmetic operators:
The address type is something that is unique to solidity. It is a data type to store a 20-byte value representing the Ethereum address. The address type comes in two versions, namely, address and address payable.
address: address type holds a 20 byte value (size of an Ethereum address).
address payable: it is same as
address, but with the additional members
The main difference between these two types is that
address payable is an address you can send Ether to, while a plain
address cannot be sent Ether.
We can implicitly convert from
address payable to
address, whereas conversions from
address payable must be explicitly done via
Explicit conversions to and from
address are allowed for
uint160, integer literals,
bytes20 and contract types. We will learn about contract types and some other types in upcoming blogs. Therefore, stay tuned to upcoming blogs.
If you want to read more content like this? Subscribe to Rust Times Newsletter and receive insights and latest updates, bi-weekly, straight into your inbox. Subscribe to Rust Times Newsletter: https://bit.ly/2Vdlld7.