Distinguish between the reference type and the original type in Java

Source: Internet
Author: User
Distinguish between the reference type and the original type in Java

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 isintThe original type. Another variable isIntegerObject 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 TypeintAnd object references each occupy 32 bits of the stack. (To indicateintOr an object reference, Java Virtual Machine implementation requires at least 32-bit storage .)IntegerThe 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 isnullThe 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 typenewYou 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 integeraAndbThe result is nothing unexpected.bThe value of is assigned to the integer variable.a, ResultaThe value is increased by 1. This output reflects what we want to happen. However, it is surprising that a value is assigned and called.setLocationAfterxAndyObject output. We are finishingx = yAssignmentAfterSpecificallyxCalledsetLocation,xAndyHow can the values be the same? After allyGrantxAnd then changedx, Which corresponds to the integeraAndbThe 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 aAndb) Is obvious. For non-original types (suchPointObject), the value assignment modifies the object reference, not the object itself. Therefore

x = y;

After,xEqualy. In other words, becausexAndyIs object reference, they are now referencing the same object. ThereforexAny 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 // 3setLocationThis method isxThe referenced object is executed. BecausexReferencedPointThe object is exactlyyThe referenced object, so we now get the following results:

BecausexAndyReference the same object, soxAll methods andyAll 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.

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.