为什么当原始类型没有对象分配时,对象分配指向的是内存位置? [英] Why do object assignments refer to memory locations when primitive types don't?

查看:36
本文介绍了为什么当原始类型没有对象分配时,对象分配指向的是内存位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于Java中基本类型的工作方式,我有一个奇怪的问题.在将对象A分配为对象B时使用对象时,就像这样

  Rectangle ObjectB = new Rectangle();ObjectA = ObjectB; 

任何对ObjectA的调用现在都引用ObjectB的内存位置.但是,当使用整数或其他原始类型时,情况并非如此.例如

  int x = 3;int y = x;整数x = 5;返回y; 

y将返回3,即初始化y时x的值.

我的问题是,为什么对对象的赋值会在内存中创建引用,而基元会相互复制值?除了这是有用的事实之外,这又如何用Java实现?

非常感谢能为我提供更多有关原始类型和对象之间分配工作原理的理解的人.

解决方案

无论变量是原始类型还是引用类型,都完成了完全相同的事情:包含在变量中的变量将被复制到另一个变量中.

唯一的区别是引用类型的变量中保存的值是对实际事物(对象)的引用,而不是对实际事物本身的引用,而对于原始类型实际事物(原始值).

说你有

  int a = 5; 

这有点像乔有一张纸(变量 a )上写着5张纸.

现在:

  int b = a; 

a a 中的 value 被复制到 b 中.这有点像玛丽走过来,拿出一张纸,然后抄下乔的纸(数字5)上的内容.

现在,说您有:

  Map a = new HashMap(); 

有点像乔的纸上写着他的地址.纸是变量 a HashMap 对象是他的房子.

现在:

  Map b = a; 

a a 中的 value 被复制到 b 中.就像玛丽走过来拿出一张纸,然后将乔的地址从他的纸上抄到纸上一样.房子尚未复制,只是有关其位置的信息.

这就是对象引用的含义:告诉JVM对象在内存中的位置的信息(如数字).

我会详细介绍它在这个答案中(Java)和这个(关于JavaScript,但是值的概念,变量和对象引用在两种语言(以及其他许多语言)中是相同的.)

I have a bit of an odd question regarding how primitive types work in Java. When using Objects when you assign an ObjectA to be ObjectB be like such

Rectangle ObjectB = new Rectangle();
ObjectA = ObjectB;

Any calls to ObjectA refer now to ObjectB's memory location. However when using integers or other primitive types this is not the case. For example

int x = 3;
int y = x;
int x = 5;
return y;

y will return 3, the value of x when y was initialized.

The question I have is why does assignment for objects create references in memory, whereas primitives make a copy of the values of each other? Other than the fact that this is useful, how is this implemented in Java?

I'd appreciate anyone who can provide me a greater understanding of how assignment works between primitive types and objects.

解决方案

The exact same thing is done whether the variables are of primitive type or reference type: The value held in the variable is copied to the other variable.

The only difference is that the value held in a variable with a reference type is a reference to the actual thing (the object), not the actual thing itself, whereas the value in the variable for primitive types is the actual thing (the primitive value).

Say you have:

int a = 5;

That's a bit like Joe having a piece of paper (the variable a) with 5 written on it.

Now:

int b = a;

The value in a is copied into b. That's a bit like Mary coming along, getting out a piece of paper, and copying down what's on Joe's piece of paper (the number 5).

Now, say you have:

Map a = new HashMap();

It's a bit like Joe having a piece of paper with his address written on it. The piece of paper is the variable a; the HashMap object is his house.

Now:

Map b = a;

The value in a is copied into b. It's like Mary coming along and getting out a piece of paper and copying Joe's address from his piece of paper onto it. The house hasn't been copied, just the information about where it is.

That's what an object reference is: Information (like a number) telling the JVM where the object is in memory.

I go into it in some detail in this answer (Java), and this one (which is about JavaScript, but the concept of values, variables, and object references is the same in the two languages [and many others]).

这篇关于为什么当原始类型没有对象分配时,对象分配指向的是内存位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆