[Solidity] Primitive Data Types, Variables

jhcha·2023년 7월 25일
0

Solidity

목록 보기
2/17
post-thumbnail

이번 글에서는 Primitive Data Types, Variables 2개의 예제에 대해서 다룬다.

Primitive Data Types

URL: https://solidity-by-example.org/primitives/

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

contract Primitives {
    bool public boo = true;

    /*
    uint stands for unsigned integer, meaning non negative integers
    different sizes are available
        uint8   ranges from 0 to 2 ** 8 - 1
        uint16  ranges from 0 to 2 ** 16 - 1
        ...
        uint256 ranges from 0 to 2 ** 256 - 1
    */
    uint8 public u8 = 1;
    uint public u256 = 456;
    uint public u = 123; // uint is an alias for uint256

    /*
    Negative numbers are allowed for int types.
    Like uint, different ranges are available from int8 to int256
    
    int256 ranges from -2 ** 255 to 2 ** 255 - 1
    int128 ranges from -2 ** 127 to 2 ** 127 - 1
    */
    int8 public i8 = -1;
    int public i256 = 456;
    int public i = -123; // int is same as int256

    // minimum and maximum of int
    int public minInt = type(int).min;
    int public maxInt = type(int).max;

    address public addr = 0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c;

    /*
    In Solidity, the data type byte represent a sequence of bytes. 
    Solidity presents two type of bytes types :

     - fixed-sized byte arrays
     - dynamically-sized byte arrays.
     
     The term bytes in Solidity represents a dynamic array of bytes. 
     It’s a shorthand for byte[] .
    */
    bytes1 a = 0xb5; //  [10110101]
    bytes1 b = 0x56; //  [01010110]

    // Default values
    // Unassigned variables have a default value
    bool public defaultBoo; // false
    uint public defaultUint; // 0
    int public defaultInt; // 0
    address public defaultAddr; // 0x0000000000000000000000000000000000000000
}
  • Data Types
    • 솔리디티 자료형은 Primitive Data Type (원시 데이터 타입), Reference Data Type (참조 데이터 타입)으로 나눌 수 있다.
    • 이 중 원시 데이터 타입은 다른 프로그래밍 언어에서 다루는 value type과 같이 변수의 저장 공간에 값을 할당하여 저장하는 방식이다.
    • 참조 데이터 타입은 값을 직접 저장하지 않고, 참조하려는 값이 저장되어 있는 주소를 저장한다.
  • Primitive Data Type
    • Boolean: bool
      • true, false 값을 저장하는 자료형
      bool b = true;
    • Integer: int
      • 정수 값을 저장하는 자료형
      int256 i = -123;
    • Unsigned Integer: uint
      • 부호가 존재하지 않는 Integer 자료형, 양의 정수만 저장한다.
      int64 i = 123;
    • bytes
      • 바이트 단위의 값을 저장하는 자료형
      bytes1 b = 0xb5; // [10110101]
    • address
      • 20 바이트 크기의 이더리움 주소 값을 저장하는 자료형
      address addr = 0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c;

Variables

URL: https://solidity-by-example.org/variables/

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

contract Variables {
    // State variables are stored on the blockchain.
    string public text = "Hello";
    uint public num = 123;

    function doSomething() public {
        // Local variables are not saved to the blockchain.
        uint i = 456;

        // Here are some global variables
        uint timestamp = block.timestamp; // Current block timestamp
        address sender = msg.sender; // address of the caller
    }
}
  • Variables (변수)
    • State Variable (상태 변수)
      • 상태 변수는 블록체인에 저장된다. 즉, 데이터가 영속성을 가지는 상태로 저장
      • 상태 변수는 함수 외부에서 선언할 수 있다.
    • Local Variable (지역 변수)
      • 지역 변수는 함수 내부에서 선언할 수 있다.
      • 선언한 함수 내부에서만 사용할 수 있으며, 블록체인에 값이 저장되지 않기 때문에 데이터의 영속성을 가지지 않는다.
    • Global Variable (전역 변수)
      • 블록체인과 관련된 정보를 제공하기 위해 내장된 변수 (ex. block, tx, msg 등)

0개의 댓글