Learn Solidity: General Value or Data Types in Solidity

In this post, we will understand the general value types or data types in Solidity programming language.

Advertisements
World is looking for a Certified Blockchain Expert Become one

Notice: This is one of the multi-post series of Learn Solidity - Build Decentralized Application in Ethereum. This is an attempt to teach you all about Solidity - A Programming Language for Ethereum Based Smart Contracts. If you want to take this as a video course please signup using below button.
pragma solidity 0.4.8; 

/*
* @title A Simple Value Type Example
* @author Toshendra Sharma
* @notice Example for the Solidity Course
* @dev This line is for developers only
* 
*/
/*
Hello guys, lets tak about the value types also known as data types in Solidity.

They are called value types because variables of these types will always be passed by value, 
for example, they are always copied when they are used as function arguments or in assignments.

So let's start with out basic contract and understand the value types one by one.

As I have already explained. Pragma will be the first line. and then we will start the contract.



*/
contract generalValueTypesContract {

    // first and the most common value type is uint. 
    // uint is mostly used for currency value or amount as there is not float or double in that case
    // it can also be used for storing Unix timestamp as it store a very large number
    // uint stores the data in 256 bits or 32 byte. Due to being unsiged it can store a very large number.
    // this is how we declare uint
    uint x;

    // you can also initialize the uint during the declaration like this
    uint x = 45;

    // int is a signed integer whose size is same like uint which is 256 bits,
    int a = 78; 

    //if you make it constant by using the constant keyword like this 
    int constant variable1 = 8;
    // then it cannot be changed after instantiation as it will be considered as constant by the compiler

    // let's specify the 256 explicitly then this will have same effect as line above. This is how we write it.
    int256 constant variable2 = 8; 

    // you can also declare and then initialize the variable in hec like this. 
    // Please note that constant will enable the compiler to relace all ouccurence of the variable in the code.
    uint constant VERSION_ID = 0x123A1; 


    // For int and uint, size can be explicitly set in steps of 8 up to 256 for example int8, int16, int24
    uint8 b;
    int64 c;
    uint248 e;

    // Let's also check the typecasting
    int x = int(b);

    // as you can see the variable b will be typecasted into int type and then stored in x


    // boolean variable can be declared by bool keyword like this
    bool b = true; 

    // or this can also be declared by 'var b = true;' for inferred typing
    // when you declare a variable using the var keyword then the variable's type will be 
    // defined by the first value which will be stored in it.
     var a = true;

    // let's see how the address can be declared. 
    // address is the soecial value type which holds 20 byte/160 bit Ethereum addresses like this
    address public owner;

    // Bytes are also available from 1-32 in size like this
    
    byte a; // byte is same as bytes1
    bytes2 b;
    bytes32 c;

    // dynamically sized bytes can also be declared like this
    
    bytes m; 
    // it is actually a special array, same as byte[] array (but packed tightly)

    // string is same as bytes, but string does not allow length or index access as of today.
    // this is how string is defined
    string n = "hello"; 
    // here string is stored in UTF8, note double quotes, not single

    // lets check out the Arrays
    // this is a static array whose length is 5 fixed
    bytes32[5] nicknames; 

    // dynamic array whose length can be changed at any time can be specified like this
    bytes32[] names; 

    // in an array you can add a new element using push function which returns new length of the array like this
    uint newLength = names.push("John"); 

    // Dictionaries can also be defined in solidity which may any type to any other type like this
    // mapping(_KeyType => _ValueType)
    mapping (string => uint) public balances;

    //please note that value inside the mapping can also be the mapping

    // Lets see Enums which are often used for state machine like this
    enum State { Created, Locked, Inactive }; 

    // post this a variable can be Declared like this
    State public state; 

    // Initializing the state can be done like this
    state = State.Created;

    // It is important to note that enums can be explicitly converted to ints like this
    uint createdState = uint(State.Locked); 


    // Define consutruct here
    function generalValueTypesContract(uint initialCoins) {
       // Initialize state variables here
    }

}
Previous: Layout of Solidity Source File Next: Ether & Time Units in Solidity