Distinguish the type and original type

zhaozj2021-02-08  474

Java offers two different types: reference types and original types (or built-in types). In addition, Java also provides a package (Wrapper) for each original type. If a constant variable is needed, is it a basic INT type or an object of the Integer class? If you need to declare a Boolean type, is it a basic Boolean or an object of the Boolean class? This article can help you make a decision. The following table lists the original types and their object package classes. Original Types and Packages Original Type Packages Boolean Boolean Char Character Byte Byte Short Short Double Double Type and the original type of behavior is completely different, and they have different semantics. For example, suppose there are two local variables in one method, a variable is an int original type, and another variable is an object reference to an Integer object: int i = 5; // Original type Integer J = New Integer (10); / / Object references These two variables are stored in a local variable table and are all operating in the Java operand stack, but their representation is completely different. (The following sections below will replace the operand stack or local variable table with a general term stack.) Original type int and object references 32 bits of the stack. (To represent an int or an object reference, Java virtual machine implementation requires at least 32-bit storage.) The stack item of the INTEGER object is not an object itself, but an object reference. All objects in Java must be accessed through an object reference. Object reference is a pointer to a certain area in the stack in the object. When declaring an original type, the storage is declared for the type itself. The front two lines of lines are represented as follows: The reference type and the original type have different feature and usage, including: size and speed problem, this type of data structure storage, when the reference type and the original type are used as a certain The default value specified when the class of instance data is specified. The default value of the object reference instance variable is NULL, and the default value of the original type instance variable is related to their type. Many program's code will include the original type and their object package. When checking if they are equal, use these two types and understand how they correct interactions and coexistence will become a problem. Programmers must understand how these two types work and interactions to avoid code errors. For example, it is not possible to call the original type, but can call the object: int J = 5; j.hashcode (); // Error // ... integer i = new integer (5); I.hashcode (); / / Correctly use the original type without calling New, no need to create an object. This saves time and space. Mixing Use the original type and objects may also result in unexpected results associated with assignment. It seems that there is no wrong code that may not 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); 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 1 b IS 2 x is java.awt.point [x = 0, y = 0] y is java. AWT.POINT [x = 1, y = 1] Performing assignment and setLocation ... a is 3 b IS 2 x is java.awt.point [x = 5, y = 5] y is java.awt.point [x = 5, y = 5] Modify the results of the integers A and B have no unexpected place. B The value is given to the integer variable A, and the value of the result A increases by 1. This output reflects what we want to happen. However, it is surprising that the output of the X and Y objects after assignment and call SetLocation. How do we call the X and Y value when the X = Y assignment is completed? After all, we give Y X, then changed X, which is nothing different from our operations on integers A and B. This confusion is caused by the use of the original type and object. It is not different from the role of these two types. But it may look different. Assign the value to the value left on the left side is equal to the value of the right. This is obvious for the original type (as previously INT A and B). For non-original types (such as Point objects), assignment modifications are object references, not the object itself. Therefore, in the statement x = Y; after, X is equal to Y. In other words, because X and Y are object references, they now reference the same object. Therefore, any changes made to X will also change Y.

转载请注明原文地址:https://www.9cbs.com/read-821.html

New Post(0)