Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does post-increment work on wrapper classes

I was doing a review of some code and came across an instance of someone post-incrementing a member variable that was a wrapper class around Integer. I tried it myself and was genuinely surprised that it works.

Integer x = 0; 
System.out.print(x++ + ", ");
System.out.print(x);

This prints out 0, 1, not 0, 0 as I would have expected. I've looked through the language specification and can't find anything covering this. Can anyone explain to me why this works and if it's safe across multiple platforms? I would have thought that this would decompose into

Integer x = 0;
int temp1 = x.intValue();
int temp2 = temp1 + 1;
System.out.println(temp1);
temp1 = temp2;
System.out.println(x.intValue());

But apparently there's something in the specification that make it add x = temp1; before the last line

like image 209
Jherico Avatar asked Nov 07 '12 23:11

Jherico


People also ask

How does post increment operator work?

The post increment operator is used to increment the value of some variable after using it in an expression. In the post increment the value is used inside the expression, then incremented by one. if the expression is a = b++; and b is holding 5 at first, then a will also hold 5.

What is the advantage of using wrapper classes?

The primary advantage of Wrapper Classes is that we need Wrapper objects to function with collections which is only possible with the help of Wrapper classes. As the wrapper classes have objects we can store null as a value. We could not store null in variables of primitive datatype.

What would happen if I will use ++ in an integer?

By a++ , a new instance of Integer is created, and the value of it comes from adding a 1 to the original Integer object which both a and b are pointing to, and then a is re-assigned to this new object.

Why all wrapper classes are final?

It is because all primitive wrapper classes (Integer, Byte, Long, Float, Double, Character, Boolean, and Short) are immutable in Java, so operations like addition and subtraction create a new object and not modify the old.


1 Answers

Answer by Sir Tedd Hopp is at a very complex level for programmers who are programming only for few or couple of years.

Let me clear your doubt in simple way suppose

    Integer x=10;
    x++;
    System.out.println(x) ;

output will be 11

Because ++ either post or pre increment is doing Addition by 1 only internally

ie x+1 is what it has to perform and put back the result in the same variable.

ie x=x+1;

now we all know that + operator can only take primitives but x is object , then we have auto-unboxing and auto-boxing concept. so the expression becomes

x.intValue()+1;//step 1 auto-unboxing

x=Integer.valueOf(x.intValue()+1);//step 2 auto-boxing

Hence output comes as 11 after another step of auto-unboxing inside the println statement.

like image 104
NAGHMAAN MOHASEEN Avatar answered Sep 30 '22 18:09

NAGHMAAN MOHASEEN