ReasonJun

Solidity : Data Type (1) 본문

Blockchain/Solidity

Solidity : Data Type (1)

ReasonJun 2023. 10. 15. 11:02
728x90

Solidity has two main types of data types: value types and reference types.

 

Value types are stored directly in the memory of the Ethereum Virtual Machine (EVM). When a value type variable is assigned to another variable, a copy of the value is created. This means that changes to one variable will not affect the other variable.

 

Reference types are stored in a separate memory location and contain a pointer to the actual data. When a reference type variable is assigned to another variable, a copy of the pointer is created. This means that changes to one variable will affect the other variable.

 

Solidity has the following value types:

  • Booleans: Boolean values can be either true or false.
  • Integers (int, uint): Integer values can be either signed or unsigned. Signed integers can be positive, negative, or zero. Unsigned integers can only be positive or zero.
  • Addresses: Addresses represent the addresses of other smart contracts or Ethereum accounts.
  • Enums: Enums are user-defined data types that represent a finite set of values.
  • Bytes: Bytes represent a sequence of bytes.
  • Strings: Strings are a sequence of bytes that can be used to represent text.

Solidity has the following reference types:

  • Arrays: Arrays are a fixed-size or dynamic-sized collection of values.
  • Structs: Structs are user-defined data types that can be used to represent complex data structures.
  • Mappings: Mappings are a data structure that maps keys to values.

In addition to value and reference types, Solidity also has the following special data types:

  • Fixed-size byte arrays: Fixed-size byte arrays are a special type of byte array that has a fixed size.
  • Contract types: Contract types represent the types of smart contracts.
  • Function types: Function types represent the types of functions.

Examples

The following examples show how to use some of the different data types in Solidity:

// Value types
bool isTrue = true;
int age = 25;
address myAddress = 0x1234567890abcdef1234567890abcdef12345678;
enum Status { Active, Inactive }
byte[] myBytes = "Hello, world!".getBytes();
string myString = "Hello, world!";

// Reference types
uint[] myArray = [1, 2, 3, 4, 5];
struct Person {
  string name;
  uint age;
} person = Person("Alice", 25);
mapping(address => uint) myMapping;

// Special data types
bytes32 myFixedSizeByteArray = "Hello, world!";
contract MyContract {}
function myFunction() {}

Data type safety

Solidity is a strongly typed language, which means that the compiler checks the data types of variables and expressions to ensure that they are compatible. This helps to prevent errors and makes Solidity code more reliable.

 

enum

// SPDX-License-Identifier: MIT
pragma solidity >= 0.7.0 < 0.9.0;

contract test {
    enum ActionChoices { GoLeft, GoRight, GoStraight, SitStill }
    ActionChoices choice;

    ActionChoices constant defaultChoice = ActionChoices.GoStraight;

    function setGoStraight() public {
        choice = ActionChoices.GoStraight;
    }

    // The enumeration is not part of the ABI, so it is the signature of "getChoice".
    // It automatically changes to "getChoice() return(uint8)".
    
        function getChoice() public view returns (ActionChoices){
            return choice;
        }

        function getDefaultChoice() public pure returns (uint) {
            return uint(defaultChoice);
        }
}

Data type

// SPDX-License-Identifier: MIT
pragma solidity >= 0.7.0 < 0.9.0;

contract Ex2_3 {
    int a; // 0
    uint b; // 0
    bool c; // false
    bytes d; // 0x
    string e; //
    address f; // 0x0000000000000000000000000000000000000000

    function assignment() public view returns(int, uint, bool, bytes memory, string memory, address) {
        return(a, b, c, d, e, f);
    }
}

 

Conclusion

Solidity data types provide a powerful way to represent and manipulate data in smart contracts. By understanding the different data types available, you can write more efficient and secure code.

728x90

'Blockchain > Solidity' 카테고리의 다른 글

Solidity : Operator / Shift operator  (1) 2023.10.15
Solidity : constant  (0) 2023.10.15
Solidity : storage, memory, calldata, stack  (0) 2023.10.15
Solidity : Variable  (0) 2023.10.15
Solidity : Smart Contract rule  (0) 2023.10.14
Comments