Abstract


  • Datatype is a classification of information and it decides the computation that can be performed on a variable

Benefits of explicit datatype

  1. Save Main Memory
  2. 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

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 the equals() 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 a Dog object) will typically result in a runtime error (such as a ClassCastException 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

Rust Compound Type