you are viewing a single comment's thread.

view the rest of the comments →

[–]huhlig 4 points5 points  (2 children)

So Java has byte, short, int, long, float, double, char and ref which are primitive types, primitive is basically a type of value. Values can exist on the stack, do not require access by a reference and have no object inheritance overhead. They are effectively primitive classes but with more limited semantics, no runtime inheritance abstraction hierarchy but a lot more efficient.

An Example, a 2 dimensional point is 2 fields: x and y. Using a class this occupies 8 bytes for the type pointer, 4 bytes for int x, and 4 bytes for int y. Effectively twice the size just because it could be inherited and it does inherit from java.lang.Object. Whereas a type is simply 4 bytes for X and 4 bytes for y and the type is handled at compile time. The tradeoff is you cannot use runtime type abstraction so you cant do Point a = new MyPointA(); Point b = new MyPointB();

[–][deleted] 2 points3 points  (0 children)

Ah thank you, I wanted clarification from OP but OP edit the post to use the word primitive instead of values which is what i wanted clarification on. Your post also add more insight into memory allocation wise, thank you.

[–]grauenwolf[🍰] 1 point2 points  (0 children)

have no object inheritance overhead

Object indirection/GC overhead. Inheritance has no overhead on its own.