0Pricing
Web3 & DApp Development Fundamentals · บทเรียน

ชนิดข้อมูลค่า

uint, address, bool, bytes

ชนิดข้อมูลค่า เป็นบทเรียน Web3 & DApp Development Fundamentals ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Web3 & DApp Development Fundamentals และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Web3 & DApp Development Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

What Are Value Types?

Value types in Solidity hold their data directly and are copied whenever they are assigned to a variable or passed to a function.

This is different from reference types (arrays, structs, mappings) which point to a shared location. Value types include bool, integers, address, and fixed-size bytes.

  • Always copied on assignment
  • Stored compactly
  • Predictable gas cost

Booleans

The bool type holds either true or false. It is the result of comparison and logical operators.

Solidity supports ! (not), && (and), || (or). Logical operators short-circuit, just like in most languages.

<code>bool isActive = true;
bool hasPaid = false;
bool ready = isActive && !hasPaid;</code>

Unsigned Integers

uint stores non-negative whole numbers. uint is an alias for uint256.

You can choose smaller sizes in steps of 8 bits: uint8, uint16, ... up to uint256. Smaller types can save storage when packed together in a struct.

<code>uint256 balance = 1000;
uint8 smallCount = 255; // max value for uint8</code>

Signed Integers

int stores values that can be negative or positive. int is an alias for int256.

An int8 ranges from -128 to 127. Use signed integers only when negatives are genuinely possible, since they are easy to misuse.

<code>int256 temperature = -15;
int8 delta = 100;</code>

Overflow Protection

Since Solidity 0.8.0, arithmetic automatically reverts on overflow and underflow. You no longer need SafeMath for basic safety.

If you intentionally want wrapping behavior for gas savings, wrap the math in an unchecked block.

<code>uint8 x = 255;
// x + 1 would revert (overflow)
unchecked {
    x = x + 1; // wraps to 0
}</code>

The address Type

An address holds a 20-byte Ethereum account identifier. It can refer to a wallet (EOA) or a contract.

The address type exposes members like .balance (in wei) and methods such as .transfer() and .send().

<code>address owner = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4;
uint256 bal = owner.balance;</code>

address vs address payable

A plain address cannot receive Ether directly. To send funds you need address payable, which adds .transfer() and .send().

You convert between them explicitly with payable(addr).

<code>address recipient = msg.sender;
address payable wallet = payable(recipient);
wallet.transfer(1 ether);</code>

Fixed-Size Bytes

Solidity offers fixed-size byte arrays from bytes1 to bytes32. These are value types and are cheaper than dynamic bytes when the length is known.

They are useful for hashes, identifiers, and short fixed data.

<code>bytes32 hash = keccak256(abi.encodePacked('hello'));
bytes1 firstByte = hash[0];</code>

Enums

An enum defines a custom type with a fixed set of named constants. Internally each value is a small uint starting at 0.

Enums make state machines readable, such as the status of an order.

<code>enum Status { Pending, Shipped, Delivered }
Status public current = Status.Pending;

function ship() public {
    current = Status.Shipped;
}</code>

Literals and Units

Solidity provides handy unit suffixes. Ether units: wei, gwei, ether. Time units: seconds, minutes, hours, days, weeks.

1 ether equals 10**18 wei.

<code>uint256 fee = 1 ether;       // 1000000000000000000 wei
uint256 timeout = 2 days;    // 172800 seconds</code>

Default Values

Solidity has no null. Every value type has a zero default: uint is 0, bool is false, address is the zero address 0x0...0.

Checking against the zero address is a common guard.

<code>address public admin; // defaults to address(0)

function setAdmin(address a) public {
    require(a != address(0), 'zero address');
    admin = a;
}</code>

Quick Check

Test your understanding of Solidity value types.

Recap

You learned the core value types in Solidity:

  • bool for true/false logic
  • uint/int with built-in overflow protection since 0.8.0
  • address and address payable for accounts and transfers
  • Fixed bytesN, enum, and unit literals like 1 ether

Remember: value types are copied, and every type has a zero default value.

คำถามที่พบบ่อย

บทเรียน “ชนิดข้อมูลค่า” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ชนิดข้อมูลค่า” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Web3 & DApp Development Fundamentals ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Web3 & DApp Development Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ชนิดข้อมูลค่า”

uint, address, bool, bytes คุณปฏิบัติ Web3 & DApp Development Fundamentals ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Web3 & DApp Development Fundamentals หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Web3 & DApp Development Fundamentals บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “ชนิดข้อมูลค่า” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Web3 & DApp Development Fundamentals นี้ได้ไหม

ได้ บทเรียน Web3 & DApp Development Fundamentals ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ชนิดข้อมูลค่า
  2. ชนิดข้อมูลอ้างอิง
  3. พื้นที่จัดเก็บเทียบกับหน่วยความจำ
  4. ค่าคงที่และค่าที่เปลี่ยนแปลงไม่ได้
← กลับไปที่ Web3 & DApp Development Fundamentals