Value Types With Details In Solidity
Hi! In this article, I will try to explain the value types in solidity language. First, let me talk about the variables in general.
→ All variables initialized will their default value. That mean there is no null and undefined.
Examples of default value types are below :
- address = 0x0000000000000000000000000000000000000000
- (u)int = 0
- bool = false
- string = “”
→ Another importent point is if we are accessing any initialized variables, then you have seen that public variables are automatically getting a getter function. So solidity creates a getter function for all public variables.
Now let’s learn value types one by one.
- Address : This is really important. Because we will usually use this value type to send eth from smart contracts to other smart contracts or to interact with smart contracts. And every transaction and everything that yo do on the theory on location is address bases.
→ This value type, holds a 20 byte value (size of an Ethereum address).
→ Addresses have members and functions you can call. Let’s talk briefly about just two:
a) balance : Basically we use it to query the balance of an address.
b) transfer : We use Ether (in wei units) to send to a payable address. - String : This value type has slightly different properties than common programming languages. For any referance types we’ve seen of the strings, you need to define the memory location either in memory or in storage. And usually almost any case, you will choose memory as in the arguments. I will talk about storage and memory in another article in detail.
Now let’s talk about some important features :
→ We cannot concatenate strings.
→ We cannot search for strings.
→ There is no string functions to replace items in strings.
→ Strings are internally stored as a bytes array. They are just converted over as UTF eight again and it is not as easy and actually is not made for really working with strings or larger strings especially.
→ It’s also quite expensive to work with strings in reality in terms of gas and gas. - (Unsigned)Integer : it is simply defined as such; signed and unsigned integers of various sizes. Keywords uint8 to uint256 in steps of 8(unsigned of 8 up to 256 bits) and int8 to int256. uint and int are aliases for uint256 and int256, respectively.
→ Integers in Solidity are restricted to a certain range. For example, with uint8, this is 0 up to 2**8-1 and int8 from -128 to 127.
→ The reason for the different sizes is mostly in the gas cost. - Booleans : The possible values are constants true and false. This variable type has operators:
→ ! (logical negation)
→ && (logical conjunction, “and”)
→ || (logical disjunction, “or”)
→ == (equality)
→ != (inequality)
Until next time!