I recently went to a Java interview, and the interviewer asked me this question. I know about boxing and unboxing, but the concept of API is very vague, and I don’t even know what the API used by boxing and unboxing is. I hope someone can help me answer it
仅有的幸福2017-07-05 10:28:57
This blog is written more clearly: Java automatic boxing and unboxing and their pitfalls
巴扎黑2017-07-05 10:28:57
Look at this piece of code first
Integer a = 1;
int b = 2;
int c = a + b;
Integer d = a + b;
This is the debugging result
In Java, only the same type can be directly calculated, but no transformation is performed here. When Integer and int are calculated in the third line, they will be automatically unboxed and converted to int type for calculation, and the fourth line will be converted to int calculation first. After the result is obtained, it is automatically packaged into Integer
大家讲道理2017-07-05 10:28:57
Take integer as an example, Integer.intValue() and Integer.valueOf(int x)