Data types in Solidity
Integer data type
There are two ways to represent an integer in solidity. They are:
- unsigned integers represented by uint: Only positive integers are allowed
- signed integers represented by int : Both positive and negative integers are allowed
The integers can be of 8,16,24,32,40...upto 256 bits
Hence the bits representation of any integer is always a multiple of 8
Thus for signed integers we can also write int8,int16,int24....int256 Similarly for unsigned integers we can also write uint8,uint16,uint24....uint256
When we just write int or uint then the compiler by default considers the variable to be of 256bits
Also when the integer variables are not initialized they take the default value as 0.
Also the compiler checks for the overflow condition. By overflow condition we mean that suppose we declare the variable with int8. Then if we initialize the variable with a value that is greater than 8 bits then the compiler gives an error.
The ranges of the int and uint are represented using the formula in the below figure:
This also makes sense because if we consider in terms of the fundamentals as the signed integers are represented with the MSB as 0 or 1 indicating positive or negative numbers thus to represent the magnitude in a 8bit integer we are left with only 7 bits and thus the range is 2^7 which is 128. On the other hand in unsigned representation there is no MSB present to denote the sign of the number(as only positive numbers are allowed) and thus all the 8 bits are available for the representation of the magnitude of number and thus the range is 0 to 2^8-1 which is 255
Summary:
String data type
The variables with the string datatype are declared as follows:
When the string variable is not initialized then in that case it is declared as ""(empty string)
One problem with the string data type is that the default behavior of string means that the string variables must always be stored inside the contract storage. However the local variables are stored in inside the memory(stack). Thus this creates a conflict. To resolve the conflict the local variables are written along with the memory keyword
Boolean data type
A variable having a boolean data type can only hold two values either true or false.
The boolean variable has a default value of false.
The syntax goes like this: