Value types

INT / UINT

uint is short for unsigned integer, and you can choose the size from uint8 to uint256

  • uint8 starts from 0 to 2 ** 8 - 1

  • uint16 starts from 0 to 2 ** 16 - 1 ...

  • uint256 starts from 0 to 2 ** 256 - 1

uint8 public u8 = 1;
uint256 public u256 = 456;
uint public u = 123; // uint is short hand for uint256

int is short for integer, and you can choose the size from int8 to int256

  • int8 starts from -2 ** 7 to 2 ** 7 - 1

  • int16 starts from -2 ** 15 to 2 ** 15 - 1 ...

  • int128 starts from -2 ** 127 to 2 ** 127 - 1

  • int256 starts from -2 ** 255 to 2 ** 255 - 1

int8 public i8 = -1;
int256 public i256 = 456;
int public i = -123; // int is short hand for int256

int and uint operators:

  • Comparisons: <=, <, ==, !=, >=, > (returns bool)

  • Bit operations: &, |, ^ (bitwise exclusive hoแบทc), ~ (bitwise negation)

  • Shifts: << (left shift), >> (right shift)

  • Addition, Subtraction and Multiplication: +, -, negative - (as in signed integer), *, /, % (modulo), ** (exponentiation)

For a type integer variable X, you can use type(X).min and type(X).max to access smallest and biggest value respectively for that type.

BOOL

bool means Boolean and has 2 possible values which are true and false

Operators:

  • ! (logical negation)

  • && (logical conjunction, โ€œandโ€)

  • || (logical disjunction, โ€œorโ€)

  • == (equality)

  • != (inequality)

The operators || and && apply the common short-circuiting rules. This means that in the expression f(x) || g(y), if f(x) evaluates to true, g(y) will not be evaluated even if it may have side-effects.

ADDRESS

  • address is a special data type in Solidity that allows storing 20 bytes (size) of the address of an EVM account

  • address payable similar to address but adds 2 more methods transfer and send

BYTES

In Solidity, the byte data type represents a sequence of bytes. Solidity has two types of bytes:

  • Fixed-size byte arrays

  • Dynamic-size byte arrays

The bytes keyword in Solidity represents a dynamic array of bytes. Essentially, it is a shorthand for byte[].

Default values

Declared variables without value assignment will have its default values.

CONTRACT

contract is used to declare a contract in Solidity.

contract can also inherit from another contract using the keyword is

ENUM

Enums are one way to create a user-defined type in Solidity. They are explicitly convertible to and from all integer types but implicit conversion is not allowed. The explicit conversion from integer checks at runtime that the value lies inside the range of the enum and causes a Panic error otherwise. Enums require at least one member, and its default value when declared is the first member. Enums cannot have more than 256 members.

The data representation is the same as for enums in C: The options are represented by subsequent unsigned integer values starting from 0.

Using type(NameOfEnum).min and type(NameOfEnum).max you can get the smallest and respectively largest value of the given enum.

TYPE

A user-defined value type allows creating a zero cost abstraction over an elementary value type. This is similar to an alias, but with stricter type requirements.

A user-defined value type is defined using type C is V, where C is the name of the newly introduced type and V has to be a built-in value type (the โ€œunderlying typeโ€)

FUNCTION

function keyword is used to declare a function in Solidity.

We can declare a function like below:

Last updated