The following table lists the original types and their object encapsulation classes. Original Type and encapsulation class
Original Type |
Encapsulation class |
Boolean |
Boolean |
Char |
Character |
Byte |
Byte |
Short |
Short |
Int |
Integer |
Long |
Long |
Float |
Float |
Double |
Double |
The behavior of the reference type and the original type is completely different, and they have different semantics. For example, assume that a method has two local variables, one of which isint The original type. Another variable isInteger Object Reference:
Int I = 5; // the original type integer J = new INTEGER (10); // Object Reference
|
Both variables are stored in the local variable table and are operated in the Java operand stack, but their representation is completely different. (The following sections in this article use general terms.StackReplace the operand stack orLocal variable table.) Original Typeint And object references each occupy 32 bits of the stack. (To indicateint Or an object reference, Java Virtual Machine implementation requires at least 32-bit storage .)Integer The stack item of an object is not an object, but an object reference. All objects in Java must be accessed through object reference. An object reference is a pointer to a region in the heap where OSS is located. When an original type is declared, the storage is declared for the type itself. The preceding two lines of code are as follows: The reference type and the original type have different features and usage, they include: size and speed problems, which type of data structure is stored, the default value specified when the reference type and original type are used as instance data of a class. The default value of the instance variable referenced by the object isnull The default values of original type instance variables are related to their types. The Code of many programs will contain both the original type and their object encapsulation. When checking whether they are equal, using both types at the same time and understanding how they interact correctly and coexist will become a problem. Programmers must understand how these two types work and interact to avoid code errors. For example, you cannot call methods of the original type, but you can call methods of objects:
Int J = 5; J. hashcode (); // error //... integer I = new INTEGER (5); I. hashcode (); // correct
|
No need to call the original typenew You do not need to create an object. This saves time and space. Mixed Use of the original type and object may also lead to unexpected results related to the assignment. It seems that no error code may fail to complete the work you want to do. For example:
import java.awt.Point;class Assign{ public static void main(String args[]) { int a = 1; int b = 2; Point x = new Point(0,0); Point y = new Point(1,1); //1 System.out.println("a is " + a); System.out.println("b is " + b); System.out.println("x is " + x); System.out.println("y is " + y); System.out.println("Performing assignment and " + "setLocation..."); a = b; a++; x = y; //2 x.setLocation(5,5); //3 System.out.println("a is "+a); System.out.println("b is "+b); System.out.println("x is "+x); System.out.println("y is "+y); }}
|
This code generates the following output:
a is 1b is 2x is java.awt.Point[x=0,y=0]y is java.awt.Point[x=1,y=1]Performing assignment and setLocation...a is 3b is 2x is java.awt.Point[x=5,y=5]y is java.awt.Point[x=5,y=5]
|
Modify integera Andb The result is nothing unexpected.b The value of is assigned to the integer variable.a , Resulta The value is increased by 1. This output reflects what we want to happen. However, it is surprising that a value is assigned and called.setLocation Afterx Andy Object output. We are finishingx = y AssignmentAfterSpecificallyx CalledsetLocation ,x Andy How can the values be the same? After ally Grantx And then changedx , Which corresponds to the integera Andb The operations are no different. This obfuscation is caused by the use of the original type and object. Assignment has no effect on the two types. But it may look all different. Assign a value to make the value on the left of the equal sign (=) equal to the value on the right. This is for the original type (such as the previousint a Andb ) Is obvious. For non-original types (suchPoint Object), the value assignment modifies the object reference, not the object itself. Therefore
After,x Equaly . In other words, becausex Andy Is object reference, they are now referencing the same object. Thereforex Any changes made will also changey . The following is the execution of the code at // 1: After the assignment at // 2 is executed, the conditions are as follows: When called at // 3setLocation This method isx The referenced object is executed. Becausex ReferencedPoint The object is exactlyy The referenced object, so we now get the following results: Becausex Andy Reference the same object, sox All methods andy All the methods executed work on the same object. It is important to distinguish between the reference type and the original type and understand the meaning of the reference. If this is not done, the written code cannot be completed. |