0

I have very basic doubt related to ArrayList Collection class.Consider the following declaration :

ArrayList<Integer> a1 = new ArrayList<Integer>();

Now, I want to know why are we using wrapper class 'Integer' here and why we can't use primitive type 'int'.I assume the answer would be so that we can perform different operations like toString etc. on the object created and to pass reference.Please correct me if I am wrong anywhere.

I executed following code to check how to pass reference but its not working.I mean method modify() should change value at index 0 if we are passing reference.....right? Please let me know what's wrong in below code:

import java.util.*;
class ArrayvsLinkedList
{
 public static void main(String args[])
 {
  int n = 1000000;
  ArrayList<Integer> a1= new ArrayList<Integer>();

  for(int i=1;i<=n;i++)
   a1.add(i);

  System.out.println("Before method called"+a1.get(0));


  new ArrayvsLinkedList().modify(a1.get(0));

  System.out.println("After method called"+a1.get(0));
 }
 void modify(Integer x)
 {
  x=x*5;
  System.out.println("Inside method "+x);
 } 
}
Bhavya Sharma
  • 309
  • 3
  • 15
  • [Why don't Java Generics support primitive types?](http://stackoverflow.com/questions/2721546/why-dont-java-generics-support-primitive-types) – Sotirios Delimanolis Apr 02 '15 at 04:23
  • Also have a look at http://stackoverflow.com/questions/8660691/what-is-the-difference-between-integer-and-int-in-java – Jayanth Apr 02 '15 at 04:26

1 Answers1

2

Generic type parameters must be reference types. ArrayList<int> is not a valid Java syntax.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Ok......could you please correct me with the code part?.....i mean how should I use modify(Integer) method to change value at index 0....i am not getting what's wrong in my code. – Bhavya Sharma Apr 02 '15 at 04:43
  • @BhavyaSharma Your modify method can't modify the Integer you are passing to it, since Integer is immutable. Even if it was mutable, `x=x*5;` wouldn't have changed the value inside the ArrayList, since Java passes parameters by value, not by reference. The only way your method can change the first element of the ArrayList is if you pass the ArrayList to it. – Eran Apr 02 '15 at 04:47
  • @Eran......I am bit confused....Please correct me with my logic...I assume a1.get(0) returns reference to an object of type Integer.....suppose it contains value 42 which is address of some Integer object with value 5.....so in that case we are passing address i.e. 42 only and hence value stored there should get modified....right? – Bhavya Sharma Apr 02 '15 at 05:03
  • @BhavyaSharma In your example, your `modify` method recieves a reference "42". When it assigns a new value to x, it changes x from "42" to some other reference to a new Integer instance that contains the value 5*5. But the array list still contains the "42" reference in its first index. – Eran Apr 02 '15 at 05:13
  • @Eran.....I got your point now....but just one more doubt.......so in this case I am passing reference i.e. 42 to modify method....right? If yes then how can I change the value at that address with this code only....I mean is there some method which we can call on Integer class and modify the value at that memory address? – Bhavya Sharma Apr 02 '15 at 10:30
  • @BhavyaSharma No there isn't, since Integer is an immutable class. This means that you can't change the value of an Integer object. If you'd replace Integer with your own class, let's call it IntegerWrapper, that contains a method setInteger() to change the integer value, you'd be able to change the state of the IntegerWrapper instance from inside a `modify (IntegerWrapper x)` method by writing something like `x.setInteger(x.getInteger()*5);`. – Eran Apr 02 '15 at 10:43
  • @Eran.....Ok....thanks a lot for the help.....:) – Bhavya Sharma Apr 03 '15 at 05:11