tags:

views:

296

answers:

7

In Java, if int is enough for a field, and if I use long for some reason, would that cost me more memory? Does that vary on types?

+3  A: 

int is 32 bit and long is 64 bit. long takes twice as much memory (which is pretty insignificant for most applications).

ifwdev
On MS C/C++ compilers, long and int are the same size.
Michael
This is language compiler and platform dependent. It will always be 32 and 64 for both java and C# though.
Taylor Leese
This answer is correct for Java.
mobrule
Yep, and I've removed my -1 and previous comments.
Jed Smith
+2  A: 

long in Java is 64bit, and int is 32bit, so obviously longs uses more memory (8 bytes instead of 4 bytes).

Omry
+7  A: 

In Java, yes, a long is 8 bytes wide and an integer is 4 bytes wide. This Java tutorial goes over the primitive data types. If you multiply the number of allocations by a certain amount (say, if you're allocating five million of these variables), the difference becomes more than negligible. For the average usage, however, it doesn't matter as much.

(You're already using Java, memory's kind of all over the place anyway.)

In native languages, there's a performance consideration; a 32-bit value can be held in a single register on a 32-bit architecture but not a 64-bit value; on 64-bit architectures, obviously, it can. I'm not sure what kind of optimization Java does on its native integers, but this might be true in its runtime as well. There's alignment issues to worry about, as well -- you see this more with using shorts and bytes, though.

Best practice would be to use the type you need. If the value will never be over 2^31, don't use a long.

Jed Smith
I'm pretty sure the speed thing applies equally to java as it does to native languages.
rmeador
I am too. Just not 'sure sure'.
Jed Smith
+2  A: 

Assuming from your previous questions that you mean to ask this in the scope of Java, the int data type is four bytes and the long data type is eight bytes.

However, whether the difference in size actually means a difference in memory usage depends on the situation.

If it's a local variable, it's allocated on the stack. As the stack is already allocated, using more stack space will not use more memory, provided of course that you don't exhaust the stack.

If it a member of a class, it will depend on how the members are aligned. Sometimes members are not stacked compactly in memory, but padding is used so that some members start at an even address. If you for example have a byte and an int in a class, there will likely be three bytes of padding between them so that the int starts at the next address divisible by four.

Guffa
+1  A: 

ifwdev guessed correctly. Java defines an int as a 32-bit signed integer, and a long as a 64-bit signed integer. If you declare a variable as a long, then yes, it will take twice as much memory as the same variable declared as an int. In general, int is typically the "default" numeric type, even for values that could be contained in smaller types like a short. Unless you have a particular reason for requiring values greater than 2^31-1, use an int.

Adam Robinson
A: 

If you know your data will fit in a specific data type (say short int in C), the only reason to use a bigger one is performance right? And if that's your goal, regardless of how marginal your performance gain is, as a general rule of thumb you want to use a size that matches your architecture's size (so for a normal 32-bit target system, you'd use a 32-bit type).

If you target more than one system, you can use a data type that matches the most often used one.

Blindy
+1  A: 
OscarRyz