DEV Community

Cover image for The 8 Primitive Types in Java

The 8 Primitive Types in Java

Jeremy Grifski on August 14, 2019

As I begin to edit my Java Basics series from a couple years ago, I thought it would make sense to start pulling some really detailed information o...
Editor guide
orenovadia profile image

Question: if I am running on a 64bit system. Does an integer take less space than a long in the java process?
If not, why shouldn't I just use long instead of int all the time?

renegadecoder94 profile image
Jeremy Grifski Author

Great question! Honestly, this is a bit outside of my systems knowledge, but I would assume that each primitive type would only take up as much space as it needs (i.e. 32 bits for an integer). That said, I'll defer to the systems experts on this one.

Here's a nice discussion that might better address your question.

In addition, I think we can take this question a step further and ask: "Why not always use something like BigInteger?"

orenovadia profile image

Thanks Jeremy.

I have done a little research on the matter.
First of all, even in 64bit systems, pointer addresses are of byte granularity (this is why you can get a specific char from a *char array in C). Because of this, it is possible to directly reference variables of less than 64 bit length. So using int instead of long actually saves memory.

In regards to runtime and performance: Arithmetic operations on 32 and 64 bit integers (on a 64 bit cpu) are done on the hardware (ALU). The time difference should not be significant, still, it is possible that adding two 32bit integers will be faster than longs because of some hardware optimizations.

In regards to BigInteger: I have to disagree on this one. BigInteger is an abstraction implemented on the software level. It must be represented by something like an array of integers, and therefore, arithmetic operations on BigInteger are not O(1) time and space complexity (probably logarithmic - because adding two BigIntegers requires you to compute each integer in an array of logarithmic size of N).

Thread Thread
renegadecoder94 profile image
Jeremy Grifski Author

Thanks for the follow up! I think a lot of what you shared followed my intuition—although I didn't know the details. Thanks again.

In terms of the BigInteger topic, I totally agree. I was trying to pose the question "why not always go bigger?", and you nailed the response. I figured the answer to that question would be similar to your original question.

harshrathod50 profile image
Harsh Rathod

Good. You wrote so much vital information.

thorstenhirsch profile image
Thorsten Hirsch no particular order

Let me order them for you. :-)

byte, char, short, int, long
float, double

ankushabhishek3 profile image

Awesome explanation...