# Data Types in Solidity smart contracts

Data types in Solidity represents the type of data the program is to use. The type can be numeric, alphanumeric, decimal, bool, etc. These attributes tell the program compiler how the programmer intends to use the data by constraining the values that a variable or a function might use. Below are common data types that are used in different programming languages.

## Data types in Solidity

Solidity is a statically typed language which means the type of each variable needs to be specified throughout your code. Data types instruct the compiler to check the usage of the variables in your contract. Declared data types have default values referred to as zero state (0). For example a boolean’s default value is False and a uint’s default value is 0. The concept of undefined or null values does not exist in solidity. So a data type will have some value (for example – hi, 123, true, etc.) or 0 as a default value. For more information read the docs.

The Solidity programming language supports different data types. Examples of these types can be found in the grid below.

Note: Byte information comes in handy when you want to optimize storage in your contract. Only use what you need because storage on the blockchain is expensive.

## Operators for Booleans

In your smart contact you can declare a bool which contains a true or false values. Then use the operators below to evaluate your data in different expressions.

For a more comprehensive list of how operators can be used with each data type you can read the docs.

## Operators for Integers

In your smart contact you can declare various integers then use the operators below to evaluate your data in different expressions.

For a more comprehensive list of how operators can be used with each data type you can read the docs.

## Operators for Fixed Point Numbers

In your smart contact you can declare various fixed point numbers then use the operators below to evaluate your data in different expressions.

For a more comprehensive list of how operators can be used with each data type you can read the docs.

Use the operators below to evaluate address data in different expressions. The operators below can be used with this type.

Note: You might not need to care about the distinction between address and address payable. You can only use address in your code. For example, if you are using the withdrawal pattern, you can (and should) store the address itself as address, because you invoke the transfer function on msg.sender, which is already set to address payable.

## Where are data types used within a contract

Data types are declared and used throughout your smart contract. Some example where you will declare a data type are:

• State Variables – In the beginning of your contract to declare data you want to save to the block chain
• Functions – Data to use within a function

The sample contract below declares several different data types. Read the comments in the code to better understand the examples.

``````pragma solidity ^0.6.0;

//different data types

contract MyContract {

//state variables at the beginning of the contract are declared and stored on the blockchain
//each variable needs to have a data type

//data type string called myString
string public myString = "hello";

//data type bytes32 is encoded and used for performance
bytes32 public myBytes32 = "hello world";

//data type int called myInt
int public myInt = 1;

//data types uint called myUint
uint public myUint = 2;

//data types uint256 called myUint256
uint256 public myUint256 = 3;
uint8 public myUint8 = 4;

//function with local variable are stored in memory.
//variables within functions have a declared data type
function getValue() public pure returns(uint) {
uint value = 6;
return value;
}``````

Try it in Remix

## Assignment Operators

These operators are for the assignment of value to a variable. The operand at the left side is variable while operand at the right side is value. Solidity supports the following arithmetic operators.

``````
pragma solidity ^0.8.7;

// Creating a contract to demonstrate how assignment operators work
contract SolidityOperatorExample {

// variables are assigned a value
// function preforms operation and changes values of variables
// after deployment execute getResults function
// review values of each variable

// Declaring variables
uint public variable_sub = 50;
uint public variable_mul = 25;
uint public variable_div = 80;
uint public variable_mod = 68;

// function that demonstrates how assignment operators work
function getRusults() public{
variable_sub -= 30;
variable_mul *= 25;
variable_div /= 5;
variable_mod %= 60;
return ;
}
}``````

Try it in Remix

This code is for learning and entertainment purposes only. The code has not been audited and use at your own risk. Remember smart contracts are experimental and could contain bugs.