Distinguish the type and original type

zhaozj2021-02-17  59

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 type and package class

Original type

Encapsulation class

BooleanBooleanCharcharactintintegerlonglongfloatFloatDoubleDoubledouble

The reference type and the original type of behavior are completely different, and they have different semantics. For example, assume that there are two local variables in one method, a variable is an int original type, and the other variable is an object reference to an Integer object:

INT i = 5; // Original type

Integer J = New Integer (10); // Object reference

Both variables are stored in a local variable table and are operated 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 code are represented as follows:

Reference Types and Original Types have different features and usage, including: size and speed problems, this type of data structure store, when the reference type and the original type are used as an instance data of a class Default. 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, you cannot call a method for the original type, but you can call the object:

INT j = 5;

j.hashcode (); // error

// ...

Integer i = new integer (5);

I.hashcode (); // correct

Use the original type without calling New, there is 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. E.g:

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 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]

The results of modifying the integer 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 that, 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. Here is the case where the code execution at // 1:

After the assignment of // 2 is as follows:

This method is performed on the object of the X reference when the setLocation is called at // 3. Because the Point objects of X reference are also the object referenced by Y, we now get the following results:

Since the X and Y references the same object, all methods to perform to X and the method of performing the same object on the same object.

It is important to distinguish between reference types and original types and understand the semantics of references. If you don't do this, you will not be able to complete the scheduled work.

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

New Post(0)