4

Below is my code:

class Demo{
public static void main(String[] args) {
    Integer i = new Integer(12);

    System.out.println(i);

    modify(i);

    System.out.println(i);

}
private static void modify(Integer i) {

    i= i + 1;
    System.out.println(i);
}

}

The OUTPUT of the above CODE is 12 12 but as I know, we are passing the wrapper object "i", it means it should be "Call by reference",Then the value should change to 13.but its 12. Any one has proper explanation for this?

ShyamD
  • 41
  • 2
  • 8

5 Answers5

9

References are passed by value, and besides of that Integer is immutable.

When passing i to the method modify the value of the reference is passed (the reference is local to that method) and when you assign another object to it you only modify that local reference/variable holding the reference. The original remains unchanged.

Immutable means that an object once created / instantiated it not possible to change its state any more.

A4L
  • 17,353
  • 6
  • 49
  • 70
7

This line of code in the modify method:

    i= i + 1;

is operating on an Integer, not an int. It does the following:

  1. unbox i to an int value
  2. add 1 to that value
  3. box the result into another Integer object
  4. assign the resulting Integer to i (thus changing what object i references)

Since object references are passed by value, the action taken in the modify method does not change the variable that was used as an argument in the call to modify. Thus the main routine will still print 12 after the method returns.

If you wanted to change the object itself, you would have to pass a mutable object (as others have mentioned, Integer is immutable) and you would have to call a mutator method or directly modify a field. For instance:

class Demo{
    static class IntHolder {
        private int value;
        public IntHolder(int i) {
            value = i;
        }
        public IntHolder add(int i) {
            value += i;
            return this;
        }
        public String toString() {
            return String.valueOf(value);
        }
    }
    public static void main(String[] args) {
        IntHolder i = new IntHolder(12);

        System.out.println(i);

        modify(i);

        System.out.println(i);

    }
    private static void modify(IntHolder i) {

        i.add(1);
        System.out.println(i);
    }

}
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
1

The wrapper classes are immutable, so operations like addition and subtraction create a new object and not modify the old.

If you want to pass a primitive by reference, one way to do it is pass a single element array:

int[] i= new int[1]{12};
...
private static void modify(int[] i) {

    i[0]= i[0] + 1;
    System.out.println(i[0]);
}
vandale
  • 3,600
  • 3
  • 22
  • 39
0

Java is always pass-by-value. The difficult thing can be to understand that Java passes objects as references and those references are passed by value.

Check it out:

Is Java "pass-by-reference" or "pass-by-value"?

Community
  • 1
  • 1
rupesh
  • 2,865
  • 4
  • 24
  • 50
0

check my eclipse it give output 12 13 12 .The modify method pass-by-value 12 then the i= i + 1; it add one the .

package com.demo.swain;

public class Demo {

        public static void main(String[] args) {
            Integer i = new Integer(12);

            System.out.println(i);

            modify(i);

            System.out.println(i);

        }
        private static void modify(Integer i) {

            i= i + 1;
            System.out.println(i);
        }

        }

Output:12
       13
       12

enter image description hereI strongly talking this code not give output 12 ,12 .

Sitansu
  • 3,225
  • 8
  • 34
  • 61
  • How is this an answer to OP's question (which was why the second output from `main` was 12 instead of 13). – Ted Hopp Dec 27 '13 at 19:07
  • @TedHopp i try my ubuntu os and eclipse IDE it give output 12 13 12 .i sure if you try this you get output 12 13 12 . modify(i); pass value 12 then intialize i=12+1 it give 13 – Sitansu Dec 27 '13 at 19:11
  • 1
    Yes, I'm not suggesting that the output is anything different than what you say. However, if all you are saying is that OP reported incorrect output, that should be posted as a comment, not an answer. (In my opinion, OP was focused on why the last output from `main()` was not 13.) – Ted Hopp Dec 27 '13 at 19:56
  • yes regarding OUT-PUT you are correct user3075488.that was not a problem.it was my mistake where i mentioned wrong output.but my question was regarding ,Whether 'Integer' works with pass by reference or not. – ShyamD Dec 28 '13 at 04:33