Home  >  Article  >  Java  >  What are the differences between reference types and primitive types in Java

What are the differences between reference types and primitive types in Java

王林
王林forward
2023-05-04 16:34:061582browse

The following table lists primitive types and their object wrapper classes.

Primitive type Encapsulated class
boolean Boolean
char #Character
byte Byte
short Short
int Integer

long  

Long
float Float
double Double

Reference types and primitive types behave completely differently, and they have different semantics. For example, suppose there are two local variables in a method, one variable is of int primitive type, and the other variable is an object reference to an Integer object:

int i = 5; // 原始类型  Integer j = new Integer(10); // 对象引用

Both variables are stored in the local variable table, And they all operate on the Java operand stack, but their representation is completely different. (The generic term stack will be used in the rest of this article instead of 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, a Java virtual machine implementation requires at least 32-bit storage.) The stack entry for 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, you declare storage for the type itself.

Reference types and primitive types have different characteristics and usages, they include: size and speed issues, which type of data structure this type is stored in, when reference types and primitive types are used as references for a certain class The default value specified when using instance data. The default value for object reference instance variables is null, while the default value for primitive type instance variables depends on their type.

The code of many programs will contain both primitive types and their object wrappers. Working with both types and understanding how they interact and coexist correctly will be problematic when checking if they are equal. Programmers must understand how these two types work and interact to avoid errors in their code.

For example, you cannot call methods on primitive types, but you can call methods on objects:

int j = 5;  j.hashCode(); // 错误  //。..  Integer i = new Integer(5);  i.hashCode(); // 正确

Using primitive types does not require calling new or creating an object. This saves time and space. Mixing primitive types and objects can also lead to unexpected results related to assignment. Code that looks bug-free may not do the job you want it 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 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]

There is nothing surprising about the results of modifying the integers a and b. The value of b is assigned to the integer variable a, resulting in the value of a being increased by 1. This output reflects what we want to happen. What is surprising, however, is the output of the x and y objects after assigning and calling setLocation. We specifically called setLocation on x after completing the assignment of x = y. How can the values ​​​​of x and y be the same? After all, we assigned y to x and then changed x, which is no different than what we did with the integers a and b.

This confusion is caused by the use of primitive types and objects. Assignment works no differently for the two types. But it may look all different. Assignment makes the value on the left side of the equal sign (=) equal to the value on the right side. This is obvious for primitive types such as int a and b earlier. For non-primitive types (such as Point objects), assignment modifies the object reference, not the object itself. Therefore, after 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 change to x also changes y. The following is the situation after the code at //1 is executed:

The situation after the assignment at //2 is executed is as follows:

When setLocation is called at //3, this method is Performed 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 refer to the same object, all methods executed on x are the same as those executed on y All act on the same object.

It is important to distinguish between reference types and primitive types and understand the semantics of references. Failure to do this will result in the code being written not being able to do its intended job.

The above is the detailed content of What are the differences between reference types and primitive types in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete