A Guidebook on Solidity Data Types: What Every Beginner Should Know

Solidity programming knowledge is crucial for developing smart contracts. There are several data types in Solidity. Read more to know about solidity data...

< Back
Back to top
  • Share

What is Solidity? 

Smart contracts play an extremely important role in the functioning of DeFi. While there are many languages like Rust and Yul, which have seen some widespread applications – Solidity has become a language of choice for smart contracts.

Built by the Ethereum Network, Solidity is a high-level, object-oriented language used to create DApps and smart contracts. Thanks to the support Solidity offers for Libraries, global variables, functions, and inheritance, Solidity has become an integral tool in every web 3.0 developer’s arsenal. 

Being a statically typed programming language, it requires programmers to add a specification for each variable during compilation. This detailed Solidity data types guide will go through the different Solidity data types on offer – along with some important topics each beginner should know about Solidity. 

Different Solidity Data Types

Different Solidity Data Types
Source: Toptal |  Different Solidity Data Types 

1) Value Types Solidity

Value-type data stores information directly in the memory. The variables that are then associated with these value types allow duplication in assignments or functions. Despite being duplicated, value-type data can maintain an independent copy of each of the duplicated variables. 

This means that if you make changes in the value of the duplicated variable, the original variable remains unaffected. Some common examples of value types in Solidity are listed as follows. 

1. Addresses

Created specifically for storage capacity of up to 20 bytes or 160 bytes, the address value type was created to correspond to the size of a typical Ethereum address. There are two different types of address values – “address” and “address Payable.”

Fundamentally, they both serve the same purpose, except for the fact that “address payable” can be used to assist in transferring Ether. 

2. Enums

Enums, abbreviated from enumerators, are the basic value types in Solidity. This data type is specifically used for defining constant values. Enums are used for improving the readability and maintenance of a smart contract.

Enums, by default, are created for the convenience of the user, and DApps don’t recognize this data type within the smart contract. It becomes integral for programmers to assign an integer value that corresponds to the Enum constant.     

3. Booleans

The Boolean value data type is an integral part of Solidity, used primarily for data types with binary results. Any “bool” data type can have two fixed values – (True/False, Yes/No). In Solidity, the default value of Boolean is false. Solidity supports all commonly used Boolean logic. 

The language, however, does not support the translation of boolean data types into integers.

4. Signed Integers

A signed integer is used to store positive/negative values in smart contracts and is usually declared using the “int” keyword. Abbreviated from “int256”, it takes up to 32 bytes of storage by default. If you choose to reduce the byte storage, you can specify the number of bits you want each integer to take up (int64, int8, etc.). It is important that you classify the number of bits in steps of 8!

5. Unsigned Integers

Unsigned integers in Solidity are used to store non-negative values (0 and above). Declared by using the keyword “uint,” Unsigned Integers also take up to 32 bytes of storage by default. Just like signed integers, you can specify the storage unsigned integers can take by specifying the number of bits (in steps of 8) that can be allocated to them.

6. Bytes

The Byte data type points to 8-bit signed integers. The Byte value type facilitates data storage in binary values (1s and 0s). The default value of a byte type is 0x00. 

2) Reference Types Solidity 

Reference type values don’t directly store values – but rather store them by referring to the address of the data’s location, without sharing it directly. The location of the data plays an important role in the amount of gas used in each transaction. Not optimizing data locations properly negatively impacts the performance of smart contracts. 

Solidity uses reference types in a unique manner – each reference variable points the users towards the location of a value data type. Two different variables could point back to the same storage location without one variable affecting the other. Some of the popular examples of reference data types are given below. 

1. Arrays

Arrays are one of the most important Solidity data types. Arrays are a collection of variables – each variable containing a unique index. This unique index can help in finding and retrieving a specific variable. The size of the array defines whether it’s fixed or dynamic.
An array stores different types of data and enables simplifying varied processes. The unique index location could help in retrieving a specifically requested variable. 

2. Array Members

Array members are usually defined in terms of length (which, in turn, determines the number of elements in one array). You can set the length using array members to facilitate size adjustments in a dynamically sized array. 

The two main tasks by Array Members are “Push” and “Pop.” The push task usually uses a member of a dynamic array and adds an element to the last position, while the pop task removes an element from the last position on the dynamic array.

3. Byte Arrays

Byte arrays are specific types of dynamically sized arrays that hold a collection of bytes together. Do not confuse byte arrays with byte value types  – the byte value type stores signed integers in a binary format, while byte arrays hold these bytes to serve different use cases. 

4. String Arrays

Dynamic arrays with unique restrictions are called String arrays within Solidity. The string array generally features characters enclosed within single/double quotes. 

5. Structs

Struct allows you to create your own data type by combining different variables of value type and reference type in a pre-defined structure.

6. Mapping

Just like hashable in other programming languages, Mapping is the most commonly used data type in the Solidity programming language. It stores the data in the form of key-value pairs, enabling you to retrieve the value using the supplied key. 

Things Every Beginner Should Know In Solidity 

1. Syntax and its Origins

Although Solidity gains inspiration from Python and JavaScript, its syntax is much more similar to C++, C, C#, and Java.  Despite being a curly bracket language, there is no manual memory management or other similar complexities that are usually associated with C++ or C#. 

In fact, the influence of Python boils down to multiple inheritances, super keywords, and modifiers, among others. 

2. Expressions and Control Structures

It has many of the same expressions and control structures as other curly bracket languages like C and C++. If, Else, for, break and return are commonly used within the Solidity programming language. 

3. Object-Oriented Language

Being an Object Oriented programming language like Python, C++, and Java, Solidity is based on the concept of classes.

4. Inheritance

Solidity supports inheritance, one of the basic Object Oriented Programming Langauge features. Inheritance is the ability to create a custom class on top of another base class, effectively increasing the functionality of the base class.

5. Programs in Solidity run on Ethereum Virtual Machine (EVM)

Just like Python is run on PVM, Solidity runs on the EVM (Ethereum Virtual machine). High-level programming languages like Solidity cannot be directly run on the CPU – as it is not compiled in binary code.

6. Solidity is Statically Typed

There are no general type variables in Solidity – meaning each variable must be explicitly defined. There are two main data types, which we had discussed in detail earlier – value types and reference types. There is no concept of null value in Solidity. Depending on the type of data, each newly declared variable will have a default value. 

7. Libraries 

The Library contains several functions which can be called later. Think of them as predefined contracts intended mainly for reuse. Following are some important characteristics of libraries:
1. A Library cannot be called directly if the state is not modified

2. Library is assumed to be stateless

3. Library cannot have state variables. 

4. Libraries cannot inherit elements.  

5. Libraries cannot be inherited

8. Global Variables and Functions

Basic functions and variables are exposed as globals and not stored in a standard library. Information about blockchains, mathematical functions, encoding, decoding, etc., are some of the most common global variables you’ll encounter. 

Conclusion

Solidity has cemented itself as the go-to language when it comes to creating smart contracts for the Ethereum blockchain. Despite other languages like Yul, Rust, or even Javascript gaining popularity, Solidity remains and will continue to remain one of the most popular languages for writing smart contracts.
The sheer flexibility it offers, the different Solidity data types, and access to libraries and other global elements are some of the key reasons why Solidity is a favorite among Web 3.0 programmers.

Hope this solidity data types guide gave you a basic understanding of what Solidity is all about. Follow our blog for more informative articles. Should you have any queries, do feel free to hit us up via our socials – we’ll be sure to help you out!

Popular Searches

What is Slope Wallet | Blockchain Forks | What are the Types of Decentralization | Physical Layer in OSI Model | How do Crypto Exchanges Work | Ethereum Upgrade | Biggest NFT Marketplace | What is Nonce in Blockchain | Types of Distributed Ledger Technology | Fiat Money Advantages | Blockchain Node Types | Staking Coins | What are Whales in Crypto | Crypto Metaverse | Is it Possible to Hack Blockchain | What are the Benefits of Blockchain | What does Proof of Stake Mean | Cloud Mining Platform | What is POW in Cryptocurrency | Examples of Altcoins

The Shard

Sign up for The Shard community newsletter

Stay updated on major developments about Shardeum.