Where and when the use of primitive data types in Java is effectively appropriate?

I wouldn't pay attention to Runtime. FreeMemory -- it's very ambiguous (does it include unused stack space? PermGen space?

Gaps between heap objects that are too small to be used? ), and giving any precise measurement without halting all threads is impossible Integers are necessarily less space efficient than ints, because just the reference to the Integer takes 32 bits (64 for a 64-bit JVM without compressed pointers) If you really want to test it empirically, have many threads recurse deeply and then wait. As in class TestThread extends Thread { private void recurse(int depth) { int a, b, c, d, e, f, g; if (depth Sleep(Long.

MAX_VALUE); } catch (InterruptedException e) {} } @Override public void run() { recurse(0); } public static void main(String _) { for (int I = 0; I.

I wouldn't pay attention to Runtime. FreeMemory -- it's very ambiguous (does it include unused stack space? PermGen space?

Gaps between heap objects that are too small to be used? ), and giving any precise measurement without halting all threads is impossible. Integers are necessarily less space efficient than ints, because just the reference to the Integer takes 32 bits (64 for a 64-bit JVM without compressed pointers).

If you really want to test it empirically, have many threads recurse deeply and then wait. As in class TestThread extends Thread { private void recurse(int depth) { int a, b, c, d, e, f, g; if (depth MAX_VALUE); } catch (InterruptedException e) {} } @Override public void run() { recurse(0); } public static void main(String _) { for (int I = 0; I.

The wrapper contains a primitive as a field, but it causes additional overhead because it's an object. The reference takes up space as well, but your example isn't really designed to show this. The tests you designed aren't really well-suited for a precise measurement, but since you used them, try this example instead: public static void main(String args) { int numInts = 100000; Integer array = new IntegernumInts; // int array = new intnumInts; for(int I = 0; I Println("Used memory (bytes): " + (Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())); } Now try it again but uncomment the primitive line and comment out the wrapper line.

You should see that the wrapper takes up much more memory.

If your first example, you have the equivalent to 1 integer, and 2 pointers. Because Integer is an Object, it has pointer properties, and contains functions. By using int instead of Integer, you are copying the value 3 times.

You have a difference in 24 bytes, which is used for storing the headers and values of your extra 2 ints. Although I wouldn't trust your test: the JVM can be somewhat random, and it's garbage collection is quite dynamic. As far as required memory for a single Integer vs int, Integer will take up more space because it is an Object, and thus contains more information.

Runtime.getRuntime().freeMemory() : getting delta on this does not give you the correct statistics as there are many moving parts like garbage collection and other threads. Integer takes more memory than int primitive.

Your test case is too simple to be of any conclusive result. Any test case that takes less than 5 seconds doesn't mean anything. You need to at least do something with these objects you are creating.

The JVM can simply look at your code and just not do anything because your objects aren't ever used, and you exit. (Can't say for certain what the JVM interpreter does, but the JIT will use escape analysis to optimize your entire testcase into nothing) First of all, if you're looking for memory effectiveness, primitives are smaller because they are what size they are. The wrapper objects are objects, and need to be garbage collected.

They have tons of fields within them that you can use, those fields are stored somewhere... Primitives aren't "designed" to be more effective. Wrapper objects were designed to be more feature friendly. You need primitives, because how else are you going to store a number?

If you really wan't to see the memory difference, take a real application. If you want to write it yourself, go ahead but it'll take some time. Use some text editor and search and replace every single int declaration with Integer, and long with Long, etc.Then take a look at the memory footprint.

I wouldn't be surprised if you see your computer explode. From a programming point of view, you need to use primitives when necessary, and wrapper objects when necessary. When its applicable to do both, it's your preference.

Trust me, there aren't that many.

javaspecialists.eu/archive/Issue193.html This might help you understand/explore things a little bit more. An excellent article! Cheers!

For a start, an Integer wraps an int, therefore Integer has to be at least as big as int. From the docs (I really doubt this is necessary): The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.So obviously a primitive int is still being used.

Not only that but objects have more overhead, and the most obvious one is that when you're using objects your variable contains a reference to it: Integer obj = new Integer(100); int prim = 100; ie. Obj stores a reference to an Integer object, which contains an int, whereas prim stores the value 100. That there's enough to prove that using Integer over int brings with it more overhead.

And there's more overhead than just that.

If you look at the source code of java.lang. Integer, the value is stored as an int. Private int value; Your test is not valid, that's all there is to it.

Private int value; in that particular version – jli Nov 3 at 21:47.

Proof: when you run these Tests you'll get an AssertionError in second Test (because memory gets lower, even if you stop resetting memory-field). Once you try this tests with 10.000 loops you'll get at both StackOverflowError. Import static org.hamcrest.MatcherAssert.

AssertThat; import static org.hamcrest.Matchers. Is; import org.junit. Test; public class TestRedundantIntegers { private long memory; @Test public void whenRecursiveIntIsSet() { memory = Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory(); recurseInt(0, 100); } private void recurseInt(int depth, int someInt) { int x = someInt; assertThat(memory,is(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())); memory=Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory(); if (depth.

As for "where and when": use the non-primitive types where an Object is required, and the primitives everywhere else. For example, the types of a generic can't be primitive, so you can't use primitives with them. Even before generics were introduced, things like HashSet and HashMap couldn't store primitives.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions