Distinction between reference types and original types in Java

Source: Internet
Author: User

Java provides two different types: The reference type and the original type (or the built-in type ). In addition, Java also provides wrapper for each original type ). If you need an integer variable, do you use the basic int type or an object of the integer class? If a boolean type needs to be declared, is it a basic Boolean or an object using the Boolean class? This article helps you make a decision.

The following table lists the original types and their object encapsulation classes.

Original Type encapsulation class

========================

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 is the int primitive type, and the other is a reference to an integer object:

Inti = 5; // Original Type

Integerj = 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 will replace the operand stack or local variable table with the general term stack .) The original type int and object reference each occupy 32 bits of the stack. (To indicate an int or an object reference, Java virtual machine must use at least 32-bit storage for implementation .) The stack item of an integer 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 variables referenced by the object is null, and the default value of the original type of instance variables is 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:

Intj = 5;

J. hashcode (); // Error

//...

Integeri = new INTEGER (5 );

I. hashcode (); // correct

You do not need to call new or create an object to use the original type. 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;

Public class test {
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" + );
System. Out. println ("B is" + B );
System. Out. println ("X is" + x );
System. Out. println ("Y is" + y );
System. Out. println ("Deming assignment and" + "setlocation ...");
A = B;
A ++;
X = y; // 2
X. setlocation (5, 5); // 3
System. Out. println ("A is" + );
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]
Ming 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]

It is no surprise to modify the results of integers A and B. The value of B is assigned to the integer variable A, and the value of result a is increased by 1. This output reflects what we want to happen. However, it is surprising that the output of X and Y objects is after the value is assigned and setlocation is called. After assigning values to X = Y, we specifically call setlocation for X. How can the values of x and y be the same? After all, we assign y to X and then change X, which is no different from the operations we perform on integers A and B.

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 obvious for original types (such as int A and B. For non-original types (such as point objects), the value assignment modifies the object reference, not the object itself. Therefore

X = y;

X equals 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. The following is the execution of the code at // 1:

After the assignment at // 2 is executed, the conditions are as follows:

When setlocation is called at // 3, this method is executed on the object referenced by X. Because the Point Object referenced by X is also the object referenced by Y, we now get the following results:

Because X and Y reference the same object, all methods executed on X and the Methods executed on Y act 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.

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.