Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1027 :
Posted: Sat Feb 02, 2013 1:18 pm
by ETS User
Isn't Integer mutable if the value lies between -128 and 127. And so the increment here will acutally affect the Integer variable?
Re: About Question enthuware.ocajp.i.v7.2.1027 :
Posted: Sat Feb 02, 2013 2:01 pm
by admin
No, none of the wrapper classes is mutable.
I think you are confusing interning with mutability. Integer objects for integers between -128 to 127 are interened. I.e. if you have the following two lines:
Integer i1 = 100;
Integer i2 = 100;
then i1 and i2 would be referring to the same Integer object.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1027 :
Posted: Sat Feb 02, 2013 2:52 pm
by Guest
Thanks Paul!
Re: About Question enthuware.ocajp.i.v7.2.1027 :
Posted: Tue Mar 23, 2021 3:33 am
by bidya74
public class TestClass{
public static Integer wiggler(Integer x){
Integer y = x + 10;
x++;
System.out.println(x);
return y;
}
public static void main(String[] args){
Integer dataWrapper = new Integer(5);
Integer value = wiggler(dataWrapper);
System.out.println(dataWrapper+value);
}
}
here the o/p: 6,20 so q is how 6 is coming since wrappers are immutable.