Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (4k points)

Since Java 5, we've had boxing/unboxing of primitive types so that int is wrapped to be java.lang.Integer, and so and and so forth.

I see a lot of new Java projects lately (that definitely require a JRE of at least version 5, if not 6) that are using int rather than java.lang.Integer, though it's much more convenient to use the latter, as it has a few helper methods for converting to long values et al.

Why do some still use primitive types in Java? Is there any tangible benefit?

1 Answer

0 votes
by (46k points)

Autounboxing can lead to hard to spot NPEs

Integer in = null;

...

...

int i = in; // NPE at runtime

In most situations the null assignment to in is a lot less obvious than above.

Related questions

0 votes
1 answer
asked Nov 4, 2019 in BI by Vaibhav Ameta (17.6k points)
0 votes
1 answer
asked Mar 23, 2021 in Java by dev_sk2311 (44.4k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...