Data types tell the computer what kind of information we want to store. They help keep data organized and safe.
Data types (uint, int, bool, address, string) in Blockchain / Solidity
uint number; // unsigned integer (only positive) int number; // signed integer (positive or negative) bool flag; // true or false address addr; // blockchain address string text; // text string
uint means the number cannot be negative.
address stores a blockchain wallet or contract location.
age.uint age = 30;int temperature = -5;
bool isActive = true;owner.address owner = 0x1234567890abcdef1234567890abcdef12345678;string name = "Alice";This smart contract shows different data types. It stores a positive number, a negative number, a true/false value, an address, and some text. The owner is set to the person who creates the contract.
pragma solidity ^0.8.0; contract DataTypesExample { uint public tokenBalance = 1000; int public temperature = -10; bool public isVerified = false; address public owner; string public greeting = "Hello, Blockchain!"; constructor() { owner = msg.sender; } }
Use uint when you know the number will never be negative.
address is special for blockchain wallets and contracts.
string stores text but can use more gas (cost) on blockchain.
uint stores positive numbers only.
int stores numbers that can be positive or negative.
bool stores true or false values.
address stores blockchain wallet or contract addresses.
string stores text messages.
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
