Solidity Value Types Explained

Amarachi Ugwu
3 min readFeb 19, 2022

--

Splash Screen

Solidity just like Java, C, C++ are statically typed languages, which implies that the data type of each of the variables should be specified and made known at compile-time instead of at run-time.

Data types allow the compiler to check the correct usage of the variables. The declared types have some default values called Zero-State, for example for bool the default value is False. Likewise other statically typed languages.

Solidity has two types, the Value types, and Reference types but I am focusing on just the Value types, check out reference types here:

Value Types

Value type variables store their own data. These are the basic data types provided by solidity. These types of variables are always passed by value and just like your guessed, the reference type variables are parsed by reference.

Pass by Value vs Pass by Reference

Value type data types

  • Boolean: This data type accepts only two values True or False.
  • Integer: This data type is used to store integer values, int and uint are used to declare signed and unsigned integers respectively.
  • Fixed Point Numbers: These data types are not fully supported in solidity yet, as per the Solidity documentation. They can be declared as fixed and unfixed for signed and unsigned fixed-point numbers of varying sizes respectively.
  • Address: Address hold a 20-byte value which represents the size of an Ethereum address. An address can be used to get a balance or to transfer a balance by using the balance and transfer method respectively.
  • Bytes and Strings: Bytes are used to store a fixed-sized character set while the string is used to store the character set equal to or more than a byte. The length of bytes is from 1 to 32, while the string has a dynamic length. Byte has the advantage that it uses less gas, so better to use when we know the length of data.
  • Enums: It is used to create user-defined data types, used to assign a name to an integral constant which makes the contract more readable, maintainable, and less prone to errors. Options of enums can be represented by unsigned integer values starting from 0.

Example

In the below example, the contract Types initialize the values of different types of Values Types.

Contract

solidity program to demonstrate value types
solidity program to demonstrate value types

Output

Output of the above solidity contract
The output of the above solidity contract

--

--