Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Generics : Type Inference Performance

I was wondering what the performance penalty is for generic type inference in Java 7. Is the "generics method type inference" cost (i.e., this) different from the "generics assignment type inference" cost (i.e., this)? I suppose the cost is negligible since type inference on generics is done in compile time?

Also, as far as "improved type inference" (i.e., this) in Java 8 (due to lambda's), does this also levy a performance hit? How is the performance cost for type inference in Java 8 different from that in Java 7?

I realize these are difficult (and possibly, ill informed) questions, and hence, welcome any input.

like image 573
OckhamsRazor Avatar asked Aug 25 '26 10:08

OckhamsRazor


2 Answers

Is the "generics method type inference" cost (i.e., this) different from the "generics assignment type inference" cost (i.e., this)?

No.

I suppose the cost is negligible since type inference on generics is done in compile time?

The cost is zero for that reason, unless you count compile time.

Also, as far as "improved type inference" (i.e., this) in Java 8 (due to lambda's), does this also levy a performance hit?

This is zero as well. There is no 'also levy a performance hit' about it.

like image 107
user207421 Avatar answered Aug 27 '26 02:08

user207421


As you supposed, generics are removed during compilation, and after that there are only pure (non-generic) classes.

The type inference in Java 7 is pretty simple to perform (of course still during compilation) so it won't impact on performance at all.

like image 35
Michal Borek Avatar answered Aug 27 '26 01:08

Michal Borek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!