0

I was doing some RnD I came across this this difference

my java code is as below

public class Main {    
public static void main(String[] args) {
    Integer x = 10;
    increment(x);
    System.out.println("print x" + x);

    List<String> strList = new ArrayList<String>();

    strList.add("one");
    strList.add("two");
    strList.add("three");
    strList.add("four");
    strList.add("five");
    strList.add("six");
    System.out.println("Before removing");
    for (String string : strList) {
        System.out.println("item " + string);
    }

    removeSomeItem(strList);
    System.out.println("After removing");
    for (String string : strList) {
        System.out.println("item " + string);
    }

}

private static void removeSomeItem(List<String> strList) {
    strList.remove(0);
    strList.remove(4);
}

private static void increment(Integer x) {
    x++;
    }
}

I got out for the above code as below

print x10
Before removing
item one
item two
item three
item four
item five
item six
After removing
item two
item three
item four
item five

my question is when I had sent Integer to function it behaved like value same way when I had sent List<String> its behaving like reference why is this difference ? can any one explain

VenomVendor
  • 15,064
  • 13
  • 65
  • 96
Sharanabasu Angadi
  • 4,304
  • 8
  • 43
  • 67
  • 2
    There is a huge debate going on in here: http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – SSC Jun 04 '14 at 07:54

3 Answers3

3

The main difference is that the Integer class is immutable, hence why you do not see the change in your main method.

x++; // this will simply return a new Integer

To see the difference, try this from your main method:

x = increment(x);

and in the increment method, change it to this:

return x++;

However, with your list example, you are simply passing a copy of the reference to the list. As long as that reference is not set to a new object (which it isn't), it is able to update the original list you passed.

JamesB
  • 7,774
  • 2
  • 22
  • 21
0

This is exactly what happened

private static Integer b;

public static void main(String[] args) {
    Integer x0 = 10;
    b = x0;
    increment(x0);
}

private static void increment(Integer x1) {
    //x1 == b is true
    x1++;  //implies x1 = x1 + 1;
    //x1 == b is now false
    //at the end of the day, you've done nothing to x0 or b
}

EDIT: This code will fail because apparently, the JVM is caching Integer values between -128 and 127, see here, set x0 = 150 and test.

public class Main {

    static Integer b;

    public static void main(String[] args) {
        Integer x = 150;
        b = x;
        increment(x);
    }

    private static void increment(Integer x) {
        System.out.println(x == b); //true
        x++;
        System.out.println(x == b);  //false
        b++;
        System.out.println(x == b);  //false
    }

}
Community
  • 1
  • 1
Olayinka
  • 2,813
  • 2
  • 25
  • 43
0

Well in case of,

removeSomeItem(strList);

you are passing the address of original ArrayList to the method so when it remove some value using that reference the original ArrayList change too (actually they are single object with two access point , I mean the reference ).

But in case of Integer you also pass the reference and the increment(Integer x) method receive the original reference. But as Integer immutable when I do something like,

x++;

In the background its work like

x=new Integer(x+1);

That’s why the original Integer remain unchanged when the ArrayList change.

Saif
  • 6,804
  • 8
  • 40
  • 61