Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

auto boxing and boxing of wrapper classes

Tags:

java

in wrapper classes we have two types of methods parseXxx() and valueOf() in every wrapper class for interconversion between primitive and wrapper objects.recently java 1.5 introduced auto boxing and boxing.so why they didn't deprecate those methods.

like image 877
satheesh Avatar asked Nov 23 '25 13:11

satheesh


2 Answers

Because Autoboxing and Auto Unboxing are just compile time features. Try writing something like this in your source file and then have a look at the decompiled code:

Integer i = 10;

Decompiled code:

Integer i = Integer.valueOf(10);

Similarly,

int i = new Integer(100);

will give you the below when decompiled:

int i = (new Integer(100)).intValue();

Thus, the JVM still heavily relies on these methods at runtime, though it's masked when you write the code.

like image 199
adarshr Avatar answered Nov 26 '25 02:11

adarshr


Well, parseXxx() is entirely unlike boxing; it turns a String into a primitive object. valueOf(), on the other hand, is actually used in boxing -- it either constructs a new wrapper object, or it fetches an existing one from a cache, depending on the value. The Java compiler generates a call to valueOf(), and that's precisely what boxing means.

like image 31
Ernest Friedman-Hill Avatar answered Nov 26 '25 02:11

Ernest Friedman-Hill



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!