Abstract
- Datatype is a classification of information and it decides the computation that can be performed on a variable
Benefits of explicit datatype
- Save Main Memory
- Generally speeds up program, refer to this video for more details. However, use it with caution, refer to the ‘Premature optimisation is the root of all evil’ below for more information
Premature optimisation is the root of all evil
Something to note is that the size of your data can affect performance as well as memory usage. CPUs are specifically designed to handle 32 bit and 64 bit values very fast, and sometimes, counterintuitively, an 8 bit value may take longer to process. So, as with everything, premature optimisation is the root of all evil.
Keep the integer as a 32 bit integer for now, if you have 10 million of them and have identified it as a problem that it uses too much memory then go down to a u8 or use bit-packing methods.
It’s actually even more nuanced than that, because of Cache Locality, so actually smaller data can be faster and slower depending on the circumstance. But that’s very complex and should be left to experimentation if the need arises.
Primitive Datatype
- Also known as Built-in Datatype
- Great performance since there isn’t much abstraction like Custom Datatype
Custom Datatype
Value comparison of custom datatype in Java
We can’t use
==
to compare OOP Object, because==
compares the value holding by the variable. However, variables are only holding the Memory Address to the OOP Object. So if we want to compare the value of OOP Object, we need to use theequals()
method.
Complex Type
- Data structures that are built upon simpler types. Array are a primary example of a complex type in this context
Rust Datatype
Rust Scalar Type
- Basically Primitive Datatype, rust supports
- 2’s Complement (补码) for Integer Encoding (数字编码) (For Integer Overflow, panic in debug mode and go back to the biggest or smallest value in release mode)
- Floating Point
- Boolean
- Character
- Refer to Rust Scalar Data Types for more details
Rust Compound Type
- Basically Custom Datatype rust supports natively
- Refer to Rust Compound Data Types for more details