What if mixing up data types could cost you real money on the blockchain?
Why Data types (uint, int, bool, address, string) in Blockchain / Solidity? - Purpose & Use Cases
Imagine you want to store different kinds of information about a user in a blockchain app: their age, whether they are active, their wallet address, and their name. Without clear rules about what kind of data each piece is, you might mix things up or waste space.
Trying to store all data without specific types is like putting all your clothes in one big box without folding or sorting. It becomes hard to find what you need, mistakes happen, and the system runs slower or costs more.
Using data types like uint (for positive numbers), int (for numbers that can be negative), bool (true or false), address (wallet locations), and string (text) helps organize data clearly. This makes your blockchain app efficient, safe, and easy to understand.
mapping(address => bytes) userData; // all data mixed in bytesstruct User { uint age; bool isActive; address wallet; string name; };
mapping(address => User) users;Clear data types let your blockchain app run faster, use less space, and avoid costly mistakes.
When sending tokens, the 'address' type ensures you send to a valid wallet, and 'uint' keeps track of token amounts without errors.
Data types organize information clearly.
They prevent errors and save resources.
Using them is essential for reliable blockchain apps.
Practice
Solution
Step 1: Understand the difference between
uintandintuintstands for unsigned integer, meaning it can only hold zero or positive numbers.intcan hold both positive and negative numbers.Step 2: Identify the correct type for only zero or positive numbers
Since the question asks for numbers that are zero or positive only,uintis the correct choice.Final Answer:
uint-> Option BQuick Check:
Positive numbers =uint[OK]
uint for non-negative numbers only [OK]- Choosing
intfor positive-only numbers - Confusing
boolwith numeric types - Using
addressfor numbers
isActive in Solidity?Solution
Step 1: Recall the correct boolean values in Solidity
Boolean variables can only be assignedtrueorfalse, not numbers or strings.Step 2: Check each option for correct boolean assignment
bool isActive = true; assignstruecorrectly. Options B and D use numbers, and C uses a string, which are invalid forbool.Final Answer:
bool isActive = true; -> Option DQuick Check:
Boolean = true or false [OK]
true or false to bool variables [OK]- Using 1 or 0 instead of true/false
- Assigning strings like "true"
- Missing semicolon at the end
int x = -5; uint y = 10; bool flag = (x < 0); string memory text = "Hello"; address addr = 0x1234567890123456789012345678901234567890; // Output values x flag text addr
Solution
Step 1: Analyze each variable's value
xis-5(signed int),flagistruebecausex < 0,textis "Hello", andaddris a valid address.Step 2: Confirm output matches variable values
-5, true, Hello, 0x1234567890123456789012345678901234567890 correctly lists all values as expected. 5, false, Hello, 0x1234567890123456789012345678901234567890 incorrectly changesxandflag. -5, false, "Hello", 0x0 has wrongflagandaddr. Compilation error due to address format is wrong because the address format is valid.Final Answer:
-5, true, Hello, 0x1234567890123456789012345678901234567890 -> Option CQuick Check:
Variable values printed as is [OK]
- Assuming uint can hold negative values
- Confusing string quotes in output
- Thinking address format is invalid
address myAddress = "0xAbC1234567890";
Solution
Step 1: Understand address assignment rules
In Solidity, addresses are assigned as hexadecimal literals without quotes, not as strings.Step 2: Identify the error in the code
The code uses quotes around the address, making it a string, which is invalid foraddresstype.Final Answer:
Address must be assigned without quotes -> Option AQuick Check:
Address literals have no quotes [OK]
- Using quotes around addresses
- Declaring address as string
- Removing 0x prefix from address
Solution
Step 1: Match each field to the correct data type
User ID is a positive number, souintfits. Username is text, sostring. Active status is true/false, sobool. Wallet address is a blockchain address, soaddress.Step 2: Verify options against these matches
uintfor user ID,stringfor username,boolfor active status,addressfor wallet matches all fields correctly. Other options mix types incorrectly, like usingintfor user ID or swappingstringandaddress.Final Answer:
uintfor user ID,stringfor username,boolfor active status,addressfor wallet -> Option AQuick Check:
Match field types carefully [OK]
- Using
intfor positive-only IDs - Confusing
stringandaddress - Using
uintfor boolean values
Ready to prove it?
Pass the test and this pattern moves unknown
