Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In Java there are two basic types in the <a href="http://java.sun.com/docs/books/jvms/second_edition/html/Concepts.doc.html#22930" rel="noreferrer">JVM</a>. 1) Primitive types and 2) Reference Types. int is a primitive type and Integer is a class type (which is kind of reference type).</p> <p>Primitive values do not share state with other primitive values. A variable whose type is a primitive type always holds a primitive value of that type.</p> <pre><code>int aNumber = 4; int anotherNum = aNumber; aNumber += 6; System.out.println(anotherNum); // Prints 4 </code></pre> <p>An object is a dynamically created class instance or an array. The reference values (often just references) are pointers to these objects and a special null reference, which refers to no object. There may be many references to the same object.</p> <pre><code>Integer aNumber = Integer.valueOf(4); Integer anotherNumber = aNumber; // anotherNumber references the // same object as aNumber </code></pre> <p>Also in Java everything is passed by value. With objects the value that is passed is the reference to the object. So another difference between int and Integer in java is how they are passed in method calls. For example in</p> <pre><code>public int add(int a, int b) { return a + b; } final int two = 2; int sum = add(1, two); </code></pre> <p>The variable <em>two</em> is passed as the primitive integer type 2. Whereas in</p> <pre><code>public int add(Integer a, Integer b) { return a.intValue() + b.intValue(); } final Integer two = Integer.valueOf(2); int sum = add(Integer.valueOf(1), two); </code></pre> <p>The variable <em>two</em> is passed as a reference to an object that holds the integer value 2.</p> <hr> <p>@WolfmanDragon: Pass by reference would work like so:</p> <pre><code>public void increment(int x) { x = x + 1; } int a = 1; increment(a); // a is now 2 </code></pre> <p>When increment is called it passes a reference (pointer) to variable <em>a</em>. And the <em>increment</em> function directly modifies variable <em>a</em>.</p> <p>And for object types it would work as follows:</p> <pre><code>public void increment(Integer x) { x = Integer.valueOf(x.intValue() + 1); } Integer a = Integer.valueOf(1); increment(a); // a is now 2 </code></pre> <p>Do you see the difference now?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload