Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to unused return values?

First I did look into this. I found the same question on here but in regards to C++: What happens to unused function return values?

My interest is in Java (though the thread was indeed interesting).

General consensus was that nothing really happens and if anything gets overridden later. Are there any differences from that thread for java?

(Links would be appreciated as I can imagine what probably happens but would like some proof & detail. Also I'm looking for a bit more depth then "It doesn't get set to a variable")

like image 953
NekoKikoushi Avatar asked Dec 15 '22 16:12

NekoKikoushi


1 Answers

The compiler sees that it is not used and just leaves the value on the (return) stack unassigned aka removes it. If you assign it but not use it, it lives until this references is not longer in use.

See the attached screenshot. Eclipse and a bytecode plugin.

Under L0, you can see that the return of the concat call is stored (ASTORE 1). The same call under L1, which in Java is not used or assigned, is just handled with a POP aka it is removed from the stack and gone.

bytecode of return handling

Hope that helps.

like image 143
ReneS Avatar answered Dec 29 '22 01:12

ReneS