Types and variables
Solidity is statically typed, so every variable has a type fixed at compile time. The value types you will use most are uint256 (a whole number from zero up), int256 (a whole number that can be negative), bool, address and bytes32. Value types are copied when you assign them.
An address identifies an account or a contract. You can read its balance with .balance and send ETH to it. A payable address is one that can receive ETH, and msg.sender is the address that called the current function.
Strings and arrays are reference types. They need a data location such as memory or calldata when used as function parameters or local variables. You will meet the three data locations in the Functions module.
Constants and immutables help you save gas. A constant is fixed in the source code, while an immutable is set once in the constructor. Both are read without touching storage.
Since Solidity 0.8, arithmetic reverts on overflow and underflow, so a uint256 never silently wraps around. If you need wrapping on purpose, you can put the math in an unchecked block, but only when you can prove it is safe.
contract Types { uint256 public constant MAX_USERS = 100; address public immutable owner; int256 public temperature = -5; bool public active = true; string public name = "solidity.in"; bytes32 public id = keccak256("solidity.in"); constructor() { owner = msg.sender; } function greet(string memory who) public pure returns (string memory) { return string.concat("Hello, ", who); } function mySelf() public view returns (address, uint256) { return (msg.sender, msg.sender.balance); } }
Try it yourself
Add a function that returns whether a given number is bigger than MAX_USERS. Then try assigning -1 to a uint256 variable and read the compiler error.
Press Compile to check your code in the browser. Short fragments are wrapped in a contract automatically.