DEV Community

Cover image for Bootcamp with blockchainbites: Session02
Jean Carlo Flores Carrasco
Jean Carlo Flores Carrasco

Posted on

Bootcamp with blockchainbites: Session02

I prepared this tutorial in order to summarize what I learned in the second class of the bootcamp with blockchainbites dictated by Lee Marreros.

Let's start with the types of variable in solidity

Solidity in comparison of Javascript is a strongly typed language.

Boolean type variable
This type of variable only can have the value of false(0) or true(1).

bool var;
Enter fullscreen mode Exit fullscreen mode

If we don't assign a value to our variable named var, this will have the value of false by default. This behavior is the same of all the types of variables.
Ex.

  • Default value for bool is false.
  • Default value for uint is 0.
  • Default value for string is "".
  • Default value for address is 0x0000000000000000000000000000000000000000, and so on.
var = true;
Enter fullscreen mode Exit fullscreen mode

This will change the value of var to true. We can declare and assign as well.

bool var = true;
Enter fullscreen mode Exit fullscreen mode

uint type variable
This means unsigned integer, so this only contains positive numbers. BTW uint is the same as uint256.

`uint`: unsigned integer = unsigned integer(positive number)
`256`: integer range.
`Range`: [0 - (2^256 -1)] equals to [0 - (1.15792089x(10^77) - 1)]
Enter fullscreen mode Exit fullscreen mode
uint256 edad = 99;
uint unsignedInteger = 100000;

Enter fullscreen mode Exit fullscreen mode

Exists another type of variable like uint8, uint128, uint152,... but they follows the same formula for the range.

`uint`: unsigned integer = unsigned integer(positive number)
`8`: integer range.
`Range`: [0 - (2^8 -1)] equals to [0 - 255]
Enter fullscreen mode Exit fullscreen mode

Address type variable
Before going to this type of variable let's mentioned that 1 byte is a unit of measurement of information that is made up of 8 bits.
A bit is the smallest unit of information and can take two values: 0 or 1. When you group 8 of these bits together, you get one byte, which can represent 256 different values ​​(2^8).

Example:
74: The first byte is represented as "74" in hexadecimal. Each of these digits represents 4 bits. In binary, "7" is represented as "0111" and "4" is represented as "0100". When combined, you get the full byte "01110100".

2d: The second byte is represented as "2d" in hexadecimal. Similarly, "2" is represented as "0010" in binary and "d" is represented as "1101" in binary. Combined, they form the byte "00101101".

Furthermore we can say that this is a unique identifier which holds a 20 bytes, which means this is made of 40 hexadecimals characters, inclusive the prefix 0x.
Each hexadecimal character represents 4 bits, so two hexadecimal character represents 8 bits or 1 byte.
Ex.
0x742d35Cc6634C0532925a3b844Bc454e4438f44e
We can see the 20 bytes here.
74 2d 35 Cc 66 34 C0 53 29 25 a3 b8 44 Bc 45 4e 44 38 f4 4e
01110100 00101101 ...

address myWallet = 0xCA420CC41ccF5499c05AB3C0B771CE780198555e;
Enter fullscreen mode Exit fullscreen mode

Visibility

Public
This means the method or variable can be used from outside of the smart contract.
This means this can be used inside the smart contract.
This means that this can be inherited.

function updateAge(uint256 newAge) public {
    age = newAge;
}
Enter fullscreen mode Exit fullscreen mode

If a variable is assigned with public, this variable automatically will have a getter and setter methods. The getter method assigned will be of the type of view. It'll be only used outside the smart contract.

uint256 public age = 0;
Enter fullscreen mode Exit fullscreen mode

View
This can help when you want to read a variable of the smart contract.

function readAge() public view returns(uint256) {
    return age;
}
Enter fullscreen mode Exit fullscreen mode

*Note: A method view only consume gas when it's called from a setter method.

uint256 age;
function getAge() public view returns(uint256) {
    return age;
}

function updateAge() public {
    age = getAge();
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)