[英]Java memory management Objects vs Primitive Types
I am getting a bit confused regarding few points regarding Java memory management. 关于Java内存管理的几点,我有些困惑。
Understood what is the difference between Stacks and Heap and I went to see a proper example on what is those memories when we execute some code. 理解了Stacks和Heap之间的区别是什么,我去看了一个有关在执行代码时这些内存是什么的正确示例。
I took for example this few lines 我以这几行为例
public static void main(String[] args)
{
int a = 5;
calculate(a);
System.out.println(a); //will print 5 not 50
}
private static void calculatee(int a)
{
a = a * 10;
}
I understood what is happening in the stack and in the heap. 我了解堆栈和堆中正在发生的事情。 The variable is not getting returned and there is no reference to the Heap in this case.
在这种情况下,不会返回变量,也没有对堆的引用。
while in this example: 而在此示例中:
public static void maina(String[] args)
{
Customer customer = new Customer("John");
setAName(customer);
System.out.println(customer.getName()); // this will return Philip
}
private static void setAName(Customer c)
{
c.setName("Philip");
}
I can see the state of the object changed this time! 这次可以看到对象的状态发生了变化!
Stacks are not shared thought threads, but heap is shared! 堆栈不是共享的思想线程,而是共享的! Which make sense to me that the object customer has changed its value from Jhon to Philip when I am printing it!
在我打印时,目标客户已将其价值从Jhon更改为Philip,这对我来说很有意义! Great!
大! all make sense!
都说得通!
However, I was expecting that if I will do this 但是,我期望如果我能做到这一点
public static void main(String[] args)
{
Integer i = new Integer(5);
calculate(i);
System.out.println(i); // printing 5 and not 50
}
private static void calculate(Integer i)
{
i = i * 10;
}
I would get 50 and not 5! 我会得到50而不是5!
In this case Integer is an object and I am assuming it is created in the heap! 在这种情况下,Integer是一个对象,我假设它是在堆中创建的!
Funny thing is that if I will wrap the Integer inside Customer I will get 50 instead of 5. 有趣的是,如果我将整数包装在客户中,我将得到50而不是5。
Why is this happening? 为什么会这样呢? Isn't the Integer type getting created in the Heap?
难道不是在堆中创建了整数类型吗?
This is a problem of references and not of heaps and stacks. 这是引用的问题,而不是堆和栈的问题。
When calling the method calculate
, you pass a reference (in your case, i
from main
). 当调用方法
calculate
,您传递了一个引用(在您的情况下, i
来自main
)。
The trick is, that Java will create a new reference inside of calculate
. 诀窍是,Java将在
calculate
内部创建一个新引用。 So i
inside of calculate
and i
inside of main
might initially "point" to the same object, but they are not the "same". 所以,
i
的内部calculate
和i
的内部main
最初可能“点”到同一个对象,但他们不是“同一”。
So, if you change i
inside of calculate
to the object resulting from your multiplication, you don't automatically change the reference of the i
inside of main
. 所以,如果你改变
i
里面的calculate
,从您的乘法产生的对象,你不会自动改变的参考i
的内main
。
With Customer
, it's different. 对于
Customer
,情况有所不同。 You never change where c
in setAName
points to. 您永远不会更改
setAName
中c
setAName
位置。 You change one reference inside of the object, but both customer
inside of main
and c
in setAName
still point to that one object! 您更改一个参考对象的内部 ,但两者
customer
的内部main
和c
在setAName
仍然指向一个对象!
Here two shitty Paint drawings to explain what I mean (numbers prefixed with 0x
are references): 这里有两张简陋的Paint图纸来解释我的意思(以
0x
数字是参考):
For the Integer example: 对于整数示例:
For the Customer example: 对于客户示例:
Don't hesitate to come back with any questions. 不要犹豫,再问任何问题。
I hope this helps. 我希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.