- Distinguish value types from reference types.
- Choose the right integer size and know its range.
- Use
address,address payable,booland fixed-sizebytes. - State the default value of every basic type.
Two Families of Data Types
| Family | Behaviour | Types |
|---|---|---|
| Value types | The variable holds the data itself. Assigning or passing it to a function makes a copy. | bool, int/uint, address, bytes1…bytes32, enum, fixed-point, function types |
| Reference types | The variable refers to where the data is stored, so two variables can point to the same data and a change through one is visible through the other (depending on the data location). | arrays, bytes, string, struct, mapping |
When a reference type is used inside a function, you must state its data location – storage, memory or calldata (Chapter 4).
Value Data Types
- Booleans – keyword
bool; only two values:trueorfalse. Operators:! && || == !=. - Signed / unsigned integers –
intstores negative and positive whole numbers;uintstores only zero and positive numbers. Sizes go from 8 to 256 bits in steps of 8 (uint8,uint16…uint256).uintis an alias ofuint256andintofint256. - Fixed-point numbers –
fixed/ufixedexist in the language but are not fully supported: they can be declared but values cannot be assigned to or from them. Decimals are therefore handled with integers (for example amounts in wei). - Address –
addressholds a 20-byte Ethereum address of a user account or contract and has members such as.balance. - Address payable –
address payableis an address that can also receive Ether through.transfer()and.send(). A plain address is converted withpayable(addr). (It is not limited to wallet addresses – a contract address can be payable too.) - Fixed-size byte arrays –
bytes1tobytes32hold exactly 1 to 32 bytes.bytes32is commonly used for hashes. - Enums – user-defined types with a fixed list of named constants (Chapter 11).
- Contract and function types – a variable can hold a contract type (e.g.
Token t) or a function reference. - Literals – fixed values written in code: numbers (
42,1e18), strings ("abc"), hex (hex"00ff"), Unicode (unicode"₹") and checksummed address literals.
Value Datatype and Keywords
| Type | Keyword | Range / Details | Default value |
|---|---|---|---|
| Boolean | bool | true / false | false |
| Unsigned integer | uint8 … uint256 | 0 to 2n−1 (uint8: 0–255) | 0 |
| Signed integer | int8 … int256 | −2n−1 to 2n−1−1 (int8: −128 to 127) | 0 |
| Address | address | 20-byte account or contract address | 0x000…000 (zero address) |
| Payable address | address payable | Address that can receive Ether via transfer/send | zero address |
| Fixed bytes | bytes1 … bytes32 | Exactly 1–32 bytes | all zero bytes |
| Fixed point | fixed / ufixed | Declared only; not fully supported | – |
Tip: type(uint8).max returns 255 and type(int8).min returns −128.
Reference Datatype
| Type | Details | Default |
|---|---|---|
| Arrays | An ordered group of elements of the same type, accessed by an index starting at 0. Size can be fixed (uint[5]) or dynamic (uint[]). (Chapter 10) | empty / all zero |
| bytes | A dynamic byte array – efficient for raw binary data of any length. | empty |
| string | Dynamic UTF-8 text, similar to bytes but without index access or length. (Chapter 9) | "" |
| struct | A user-defined group of variables of different types (a record). A struct cannot contain a member of its own type directly. (Chapter 13) | each member at its default |
| mapping | Key-value storage like a hash table or dictionary; every possible key exists and returns the default value until set. (Chapter 12) | default value for every key |
Example : Declaring Different Types
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract DataTypesDemo {
// value types
bool public isActive = true;
uint8 public smallNum = 255; // maximum value of uint8
int256 public temperature = -12;
address public owner = msg.sender; // 20-byte address
bytes32 public fileHash = keccak256("Solidity");
// reference types
uint[] public marks; // dynamic array
string public courseName = "Solidity";
mapping(address => uint) public balances; // key => value
function limits() public pure returns (uint8 maxU8, int8 minI8) {
return (type(uint8).max, type(int8).min); // (255, -128)
}
}
Key Takeaways
- Value types are copied; reference types need a data location and may be shared.
uint=uint256; choose smaller sizes only when you know the range.- Every variable has a default value – there is no “undefined” or “null” in Solidity.
The PDF E-Notes are the earlier printable version of this chapter. The notes on this page are the latest, revised version.
Question / Answer (15 questions)
.balance.address payable has the .transfer() and .send() members for sending Ether to it. A plain address is converted with payable(addr).uint is an alias for uint256, and int is an alias for int256.type(T).max, for example type(uint8).max returns 255; type(T).min returns the minimum.fixed and ufixed can be declared but values cannot be assigned to or from them, so decimals are represented with integers, such as amounts in wei.mapping(KeyType => ValueType) name;, for example mapping(address => uint) balances;.MCQ Test : Check Your Understanding (5 questions)
Select an option and press Check Answer, or answer all questions and press Submit Test to see your score. Each answer comes with an explanation.
Bibliography/References
The notes, questions and MCQs on this page are original explanations written for this course. The works listed below were consulted and are cited for attribution and further reading. Product names, logos and trademarks belong to their respective owners. Short quotations, where used, are marked and attributed.
- Solidity Team. (n.d.). Types (value types, reference types, mappings, operators, conversions). Solidity Documentation. https://docs.soliditylang.org/en/latest/types.html
- Solidity Team. (n.d.). Cheatsheet. Solidity Documentation. https://docs.soliditylang.org/en/latest/cheatsheet.html
- Modi, R. (2018). Solidity Programming Essentials: A beginner’s guide to build smart contracts for Ethereum and blockchain. Packt Publishing. ISBN 978-1-78883-138-3.
- Antonopoulos, A. M., & Wood, G. (2018). Mastering Ethereum: Building Smart Contracts and DApps. O’Reilly Media.