title: Java int memory usage
body: <p>While I was thinking over the memory usage of various types, I started to become a bit confused of how Java utilizes memory for integers when passed to a method.</p>
  
  <p>Say, I had the following code:</p>
  
  <pre><code>public static void main (String[] args){
       int i = 4;
       addUp(i);
  }
  
  public static int addUp(int i){
       if(i == 0) return 0;
       else return addUp(i - 1);         
  }
  </code></pre>
  
  <p>In this following example, I am wondering if my following logic was correct:</p>
  
  <ul>
  <li>I have made a memory initially for integer i = 4. Then I pass it to a method. However, since primitives are not pointed in Java, in the addUp(i == 4), I create another integer i = 4. Then afterwards, there is another addUp(i == 3), addUp(i == 2), addUp(i == 1), addUp(i == 0) in which each time, since the value is not pointed, a new i value is allocated in the memory.</li>
  <li>Then for a single "int i" value, I have used 6 integer value memories.</li>
  </ul>
  
  <p>However, if I were to always pass it through an array:</p>
  
  <pre><code>public static void main (String[] args){
       int[] i = {4};
       // int tempI = i[0];
       addUp(i);
  }
  
  public static int addUp(int[] i){
       if(i[0] == 0) return 0;
       else return addUp(i[0] = i[0] - 1);         
  }
  </code></pre>
  
  <p>- Since I create an integer array of size 1 and then pass that to addUp which will again be passed for addUp(i[0] == 3), addUp(i[0] == 2), addUp(i[0] == 1), addUp(i[0] == 0), I have only had to use 1 integer array memory space and hence far more cost efficient. In addition, if I were to make a int value beforehand to store the initial value of i[0], I still have my "original" value.</p>
  
  <p>Then this leads me to the question, why do people pass primitives like int in Java methods? Isn't it far more memory efficient to just pass the array values of those primitives? Or is the first example somehow still just O(1) memory?</p>
  
  <p>And on top of this question, I just wonder the memory differences of using int[] and int especially for a size of 1. Thank you in advance. I was simply wondering being more memory efficient with Java and this came to my head.</p>
  
  <blockquote>
    <p>Thanks for all the answers! I'm just now quickly wondering if I were to "analyze" big-oh memory of each code, would they both be considered O(1) or would that be wrong to assume?</p>
  </blockquote>