Abstract
- Each type has a set of Operation associated with it
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 block 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
- In Java, primitive datatypes are Immutability
Custom Datatype
- In Java, custom datatypes aren’t Immutability
- Examples are OOP Class and Struct
Java Value Comparsion
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
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
Rust Scalar Type
- Basically Primitive Datatype, rust supports
- 2’s Complement (补码) Integer (整数) (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
Terminologies
Statically Typed
- The Datatype must be known during Compilation, either by explicitly define the datatype or assign a value to the variable, so the language is able to infer
Infer
Only infer when the value assigned shows the datatype, for example
a := "thisIsStr"
, always specify type when it isn’t obvious likevar a := foo()
Safer codes and more informative coding experience
Compiler has more information to do more checks on the codes and enforce certain standards. Plus better code competition when coding. Refer to this video for more more details and example.