When the Java compiler autoboxes a primitive to the wrapper class, what code does it generate behind the scenes? I imagine it calls:
- The valueOf() method on the wrapper
- The wrapper’s constructor
- Some other magic?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use the
javaptool to see for yourself. Compile the following code:To compile and disassemble:
The output is:
Thus, as you can see, autoboxing invokes the static method
Integer.valueOf(), and autounboxing invokesintValue()on the givenIntegerobject. There’s nothing else, really – it’s just syntactic sugar.