1

I'm very new to actual programming not as much blockchain. I know a little Java and am trying to learn solidity I am reading through the Intro to smart contracts turotial and on the 7th line (see below) they declare a variable using mapping but I don't understand how the => comes into play.

mapping (address => uint) public balances;
Mbando
  • 117
  • 2
  • 9

2 Answers2

3

In an array with key value pair; you can refer to a value using the corresponding key.

i.e. array[key] will return the value stored.

In solidity mapping, can be think of as an array and here in solidity syntax mapping (address => uint) public balances; means that balances has set of uint data type values that are mapped by address data type.

Basically you can get a corresponding uint value for a given address from balances. To get a corresponding value here you need to call balances[address] and that will return the uint value.

You can find a detailed explanation in the solidity docs here.

Mapping types are declared as mapping(_KeyType => _ValueType). Here _KeyType can be almost any type except for a mapping, a dynamically sized array, a contract, an enum and a struct. _ValueType can actually be any type, including mappings.

Mappings can be seen as hash tables which are virtually initialized such that every possible key exists and is mapped to a value whose byte-representation is all zeros: a type’s default value. The similarity ends here, though: The key data is not actually stored in a mapping, only its keccak256 hash used to look up the value.

And this question will also be a good read on how mapping works.

Achala Dissanayake
  • 5,819
  • 15
  • 28
  • 38
2

mapping is similar to Dictionary in Java where you specify key and value pair like

var mydictionary = new Dictionary(key,value);

while in solidity you use the same with => ; where Key = address and uint as value.

Rangesh
  • 345
  • 1
  • 6