Value Types In Solidity

Reading Time: 3 minutes

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.

Introduction To Solidity - DEV Community

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.

Value Type

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.

Boolean

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 trueop2(y) will not be evaluated.

Integer

The integer type is denoted by keywords uint8 to uint256 and int8 to int256 for unsigned and signed integers respectively. uint and int are aliases for uint256 and int256, respectively.

The operations which we can perform on integers are –

  • Comparisons: <=<==!=>=> (result value is bool)
  • Bit operators: &|^ (bitwise exclusive or), ~ (bitwise negation)
  • Shift operators: << (left shift), >> (right shift)
  • Arithmetic operators: +-, unary - (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 – fixedMxN and ufixedMxN. Here  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 –

  • Comparisons: <=<==!=>=> 
  • Arithmetic operators: +-, unary -*/%

Address

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 transfer and send.

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.

Type conversions:

We can implicitly convert from address payable to address, whereas conversions from address to address payable must be explicitly done via payable(<address>).

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.

rust-times

Knoldus-blog-footer-image

1 thought on “Value Types In Solidity4 min read

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading