Sandro Dolidze
Sandro Dolidze

Reputation: 177

Object value doesn't change

class smth{
    public static void main(String[] args){
        private Integer x = new Integer(17);
        inc(x);
        System.out.print("x="+x);
    }
    public static void inc(Integer x){
        x++;
        System.out.println("n="+x);
    }
}

output: n=18; x=17;

Integer is an object and I don't understand why the value of x did not change in this case.

Upvotes: 2

Views: 1278

Answers (7)

dreamcrash
dreamcrash

Reputation: 51593

It is because the object modifying inside the method incis not the same as the one printed outside this method.

Inside the method inc x is a reference which points to an object. When you run x ++, that reassigns X to reference a new Integer object, with a different value. Thus, you are not modifying your original 'x' variable declared on the main.

You have to return the "pointer" to the new integer object:

public static int inc(Integer x){
          x++;
          System.out.println("n="+x);
          return x;
      }

  public static void main(String argv[])

              Integer x = new Integer(17);
              x = inc(x);
              System.out.print("x="+x);  
  }

An Integer, is a Object that contains a single int field. An Integer is much bulkier than an int. It is sort like a Fedex box to contain the int. Integers are immutable (source)

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726987

This is because Integer, a wrapper over the primitive int, is immutable, and Java passed object references by value. Any change made to the object reference inside the method has no effect on the object passed in: the reference is replaced with a reference to a new object, yet the original remains intact because it is immutable.

To address this issue, you need another level of indirection - you can use an array, a mutable int from apache commons, or roll your own suitably designed class.

public static void main(String argv[])
    MutableInt x = new MutableInt (17);
    inc(x);
    System.out.print("x="+x.intValue());  
}
public static void inc(MutableInt x){
    x.add(1);
    System.out.println("n="+x.intValue());
}

Upvotes: 1

amphibient
amphibient

Reputation: 31338

It is because Java passes objects to methods by value and not by reference. Upon completing the method call, the object value will be the same in the calling method as it was before passing it. Within the called method, the value can change but the scope of the change is the method to which it was passed.

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 692121

Because x++, on an Integer object, actually means:

int temp = x.intValue();
temp++;
x = Integer.valueOf(temp);

The x variable is being assigned a new Integer instance. And since parameters are passed by value, the original reference stays unmodified.

Upvotes: 1

bhuang3
bhuang3

Reputation: 3643

Any wrapper classes are immutable class.

Upvotes: 1

bellum
bellum

Reputation: 3710

Cause Integer is immutable object. When you send it to method, new reference to it is created. When you do increment, reference inside method is reassigned to new Integer with value 18 but reference inside main is still referencing to old Integer with value 17.

Upvotes: 3

NPE
NPE

Reputation: 500883

Integer is immutable. The x++ creates a new object, and rebinds x to refer to it. The change does not propagate back to the caller since the reference to the original x was passed by value.

Upvotes: 1

Related Questions