I have seen a video of someone saying that we should be using primitives instead of objects as much as possible. The reason is that accessing an object means using a pointer and so there is an overhead.
Therefore:
- if there is a class mapped with a SQL table (using JPA) with several fields being Objects because of potential null values in database (e.g: Integer instead of int)
- Should I create a mother class where Objects are replaced with primitives, when possible, using fixed values as equivalent to null in my app ? (e.g: -1 instead of null)
- The mother class being used when processing large amount of data
- And the child class mapped to the database, only used at the time of persisting data
- In the child class I could add some methods mapped to columns, like below:
@Column("MY_COLUMN")
public Integer getId() {
// super.getIdAsInt() returns a value of type int, the primitive.
// -1 means value not set in my app.
Integer result = (super.getIdAsInt() == -1) ? null : super.getIdAsInt();
return result;
}
I think this pattern could also be applied for any cases where the child class is about persisting data (into a database or an XML file, ...) and fields with null values are meaningful.
- Do you think these could be best practices in order to improve performances of an application?
- If not, could you give me some examples of potential drawbacks?
Thanks,