available starting from J2SE 5.0 the basic data type's own active boxing (autoboxing), unpacking (unboxing) function.
What is your own active boxing:
When we create an integer object, we can do this:
Integer i = 100; (Note: not int i = +;)
In fact, when we ran the code above, the system ran for us: integer i = new integer (100); This is the self-active boxing feature of the base data type.
What's your own initiative to dismantle the box
Take the initiative to unpack (unboxing), that is, the basic data in the object from the object itself out of the initiative. For example, the following can be implemented by self-unpacking:
Integer integer=100;
int Flag=integer; This statement realizes the self-unpacking.
in the compiling , the compiler can infer whether or not to carry out its own active boxing action according to the statement you wrote.
Error One:nullpointerexception
For example, the following program will compile without error, at the time of execution is reported nullpointerexception
Integer i = null;int j = i;
This syntax is legal at compile time, but there will be errors during execution, as this is equivalent to:
Integer i = null;int j = i.intvalue ();
Null indicates that I did not reference the object entity to whatever. It can legitimately assign a name to an object. Because I was not actually involved in any of the objects, it would be impossible to manipulate the intvalue () method so that the above notation would be executed with a nullpointerexception error.
Error Two: Nosuchmethoderror
Java at compile time, it will take the initiative to carry out the boxing unboxing operation, when we compile the end of the generated. class file. The. Class binary definitely treats both the integer object and the base data type differently.
Error Recurrence:
Step one: Compiling the Student class
public class Student {public int getstudents (int flag) { return flag;} }
Step two: Compile and execute the teacher class. No matter what the problem
public class Teacher {private static Student student=new Student ();p ublic static void Main (string[] args) {System.out.prin TLN (Getteachercount (3));} public static int getteachercount (integer integer) {return student.getstudents (3);}}
Step Three:
We change the student class. and compile
<pre name= "code" class= "java" style= "Color:rgb (51, 51, 51); line-height:20px; ">public class Student {public integer getstudents (integer integer) { return integer;} }
Step four.
execute the Main method in the teacher class again, error Nosuchmethoderror.
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Java's own initiative to open the box can easily lead to two of myths