Datatypes in Solidity

What are the various Datatypes in Solidity?

As we know Solidity is similar to languages like Javascript and Python. Hence, datatypes in Solidity are quite like Javascript. Unlike Javascript, Solidity is a statically typed language. A statically typed language means that you have to specify the data type of any variable before assigning it a value, this is similar to how languages like Java work where we have to specify the type of data of the value in the variable. So, if we want to assign the value of an integer to a variable, we have to define the variable with the data type 'int'. Let's take a look at the different data types in Solidity.

Integer: This datatype is used to store numbers in the variable. We use uint and int to assign the unsigned and signed integers respectively

Boolean: This datatype takes only two values, that are true and false

Fixed Point Numbers: This datatype is not supported fully by Solidity yet. However, as per the Solidity documentation, it can be used to set signed and unsigned integers as fixed and unfixed.

Address: An address is used to store a 20byte value of an Ethereum account. It can be used to get the balance of the account or transfer the balance from one account to another by using the balance and transfer methods.

Bytes: A byte is used to store a fixed-character size. Using bytes are an advantage as they use less gas, so if we know the exact byte size of a value we can use Byte to lower gas fees significantly

String: It is used to set a character set equal to or more than a byte, i.e 32.

Enums: It is used to set integral constants which make the contract more readable and less prone to more errors.

pragma solidity ^0.8.4

contract dataTypes {

// Initializing a Boolean Variable 

bool public boolean = true;

// Intializing a Integer Variable

int32 public integer_var = -123;

// Initializing a String Variable

string public str = 'Suyash';

//Initializing a Byte Variable

bytes1 public c = 'a'; 

}

If you have any questions, you can find me on my Twitter and LinkedIn