Back to homepage

Solidity fundamentals: basic data types

Published

Solidity is a typed programming language which means we have to indicate the type of values we're going to store in our variables. So it makes sense that one of the first thing to learn is the different data types we can use to structure our application's data. Find them below 👇

Integers

Integers are used to store numbers. They're defined with the uint or int keywords. See below some examples:

pragma solidity >=0.6.0

contract MyContract {

  uint age = 36;
  int accountBalance = -2500;


}

The uint data type is an unsigned integer, meaning its value must be non-negative whilest the int data type is used for signed integers which can be negative.

By default, all integers are represented in 256 bits (or int256/uint256) but you can specify integers with less bits by using the specific keyworkds uint8, uint16, uint32, etc..

Numbers with decimals

It's important to notice that Solidity does not support floating numbers so it's common practice to define the number of decimals in our contracts as you can see in the example below:

pragma solidity >=0.6.0

contract MyContract {

  // No decimals in price itself 🤨
  uint price = 299;
  // Number of decimals for the price
  uint decimals = 2;

}

With this information, in our front end we could calculate that whatever value we got for price, we'll get the latest two positions as decimals and the price would be 2.99 instead of 299.

Math operations in Solidity

I've explained how to do the most common math operations with numbers in Solidity in the following article

Strings

String variables are used to store arbitrary-length text and they're defined with thr string keyword. For example:

pragma solidity >=0.6.0

contract MyContract {

  string name = 'Antonio';
  string bookTitle = 'the Lord of the Rings';
  string bookSummary = 'Some hobbits, men, elves, dwarves, and a wizard have to destroy a ring to prevent evil and reclaim the throne of men.'

}

We have specific articles on how to compare strings and how to concatenate strings in solidity.

Booleans

Boolean variables are used to store true/false values and are defined with the bool keyword: Example:

pragma solidity >=0.6.0

contract MyContract {

  bool taskCompleted = false;
  bool SolidityTips_is_awesome = true;

  function completeTask() public {
    taskCompleted = true;
  }


}

Addresses

You can think of the address data type as your bank account number. It's a unique identifier that represents an account and are used to send and receive Ether payments. Address variables are defined with de address keyword.

In addition, addresses can be of two types:

  • address: contains the 20 byte value of the address in the blockchain.
  • address payable: similar to the address but it also contains the transfer and send methods which are used to transfer ETH.
pragma solidity >=0.6.0

contract MyContract {

  address myAccount = '0x2ae98fE488C85f77996648aFD5C59125e7C2345C';
  address payable yourAccount = '0x0aC646595386F92DF41614C46F1d6df7ac969345'

}

These are the most basic data types but there are more complex types like arrays, struts and mappings that I've explained in the following article.

TAGS

If you enjoyed this article consider sharing it on social media or buying me a coffee ✌️

Buy Me A Coffee