Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to distinguish between reference type and original type in Java

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to distinguish between the reference type and the original type in Java. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something from this article.

Distinguish between reference type and original type in Java [@ more@]

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

Primitive types and wrapper classes

Primitive type wrapper 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, suppose a method has two local variables, one of which is of the int primitive type and the other of which is an object reference to an Integer object:

Int I = 5; / / primitive type Integer j = new Integer (10); / / object reference

Both of these variables are stored in the local variable table and operate on the Java Operand stack, but they are represented quite differently. (in the following sections of this article, the generic term stack will be used instead of the Operand stack or local variable table. The primitive type int and the object reference each occupy 32 bits of the stack (to represent an int or an object reference, the Java virtual machine implementation requires at least 32-bit storage. The stack item of an Integer object is not the object itself, but an object reference

All objects in Java are accessed through object references. An object reference is a pointer to an area in the heap where the object is stored. When you declare a primitive type, the storage is declared for the type itself.

Reference types and primitive types have different characteristics and uses, including size and speed issues, which type of data structure this type is stored in, and the default values specified when the reference type and the original type are used as instance data for a class. The default value of object reference instance variables is null, while the default values of primitive type instance variables are related to their types.

The code of many programs will contain both the original types and their object encapsulation. When checking whether they are equal, it becomes a problem to use both types at the same time and to understand how they correctly interact and coexist. Programmers must understand how these two types work and interact to avoid code errors.

For example, you cannot call a method on a primitive type, but you can call a method on an object:

Int j = 5; j.hashCode (); / / error / /. Integer I = new Integer (5); i.hashCode (); / / correct

There is no need to call new or create an object to use the original type. This saves time and space. Mixing primitive types and objects can also lead to unexpected results related to assignment. It seems that code without errors may not do what 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 (0Power0); Point y = new Point (1 is 1); / / 1 System.out.println ("an 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; axioms; x = y; / 2 x.setLocation (5Power5); / / 3 System.out.println ("an is" + a); System.out.println ("b is" + b); System.out.println ("x is" + x); System.out.println ("y is" + y);}}

This code produces the following output:

An is 1 b is 2 x is java.awt.Point [Xerox 0menyyong0] y is java.awt.Point [xxiang1reyyong1] Performing assignment and setLocation... An is 3 b is 2 x is java.awt.Point [Xero5Cindy 5] y is java.awt.Point [Xero5Lyrie 5]

It is not surprising to modify the results of integers an and b. The value of b is assigned to the integer variable a, and as a result the value of an increases by 1. This output reflects what we want to happen. What is surprising, however, is the output of the x and y objects after the assignment and the call to setLocation. After we finish the x = y assignment, how can the values of setLocation,x and y be the same when we deliberately call x? After all, we assign y to x and then change x, which is no different from what we do with integers an and b.

This confusion is caused by the use of primitive types and objects. There is no difference in the role of assignment between the two types. But it may look all different. Assign a value so that the value to the left of the equal sign (=) equals the value on the right. This is obvious to primitive types such as int an and b above. For non-primitive types, such as Point objects, the assignment modifies the object reference, not the object itself. Therefore, after the statement x = y;, x equals y. In other words, because x and y are object references, they now refer to the same object. Therefore, any changes made to x will also change y. Lower

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.

Because x and y refer to the same object, all methods executed on x act on the same object as methods executed on y.

It is important to distinguish between reference types and primitive types and to understand the semantics of references. Failure to do this will prevent the written code from completing the intended work.

After reading the above, do you have any further understanding of how to distinguish between reference types and primitive types in Java? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report