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.
Type Casting
- Also known as Type Conversion
- The process of converting a variable from one data type to another. This allows the variable to be treated as a different data type and utilise its properties. Type conversion can be implicit, where the conversion is handled automatically by the programming language, or explicit, where the conversion is performed manually by the programmer.
Explicit type casting is dangerous
Data Loss: Converting from a floating-point number (
float
) to an integer (int
) results in the truncation (loss) of the decimal portion.Loss of Object State or Methods: Widening type conversions can lead to the loss of access to specific subclass attributes and methods.
Invalid Casting: Incorrectly casting objects (e.g., trying to treat a
Cat
object as if it were aDog
object) will typically result in a runtime error (such as aClassCastException
in Java).
Widening Type Conversion
- Casting a subclass object to its superclass
- A value of datatype can be assigned to a variable of type if and only if is a subtype of
- Supported by Java
Struct
- Allows us to group a set of data to form our own Datatype. Refer to Struct in GO to see how struct is implemented and used in Go
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