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

1

3 Answers 3

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.

4
  • it increment the value of x by 1, right ? x++ is equivalent to x = x + 1 Jun 4, 2014 at 7:56
  • 1
    Yes, but not to the original object reference you passed, it has created a new object which is immediately discarded when the method ends.
    – JamesB
    Jun 4, 2014 at 7:57
  • Thanks @JamesB, so you are saying that whenever we are sending List it will be reference not value Jun 4, 2014 at 8:03
  • @SharanabasuAngadi You get a copy of the reference. I know terminology is confusing but everything in Java is pass by value.
    – JamesB
    Jun 4, 2014 at 8:04
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
    }

}
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.