问题描述
我认为我理解了java中局部和全局变量之间的区别,直到我今天看到一个例子。在此代码中,我们尝试将元素添加到方法中的链接列表中:
I thought I understood the difference between local and global variables in java until I saw an example today. In this code one tries to add elements to a link list in a method:
public void addDataPacket(DataPacket data){
PacketQueueElement newElement = new PacketQueueElement(data);
if(firstElement != null){
lastElement.setNextElement(newElement);
lastElement = newElement;
}
else{
firstElement = newElement;
lastElement = newElement;
}
}
我不明白为什么newElement会这样做方法关闭后不会消失?因为这是一个局部变量,并且没有在类中的任何地方定义。以下是此类的完整代码:
What I don't understand is why the newElement does not disappear after the method is closed? Because this is a local variable and is not defined in the class anywhere. Here is the complete code of this class:
public class PacketQueue {
/** Das erste Element in der Warteschlange */
private PacketQueueElement firstElement;
/** Das letzte Element in der Warteschlange. */
private PacketQueueElement lastElement;
/**
* Instanziert eine neue Warteschlange.
*/
public PacketQueue(){
this.firstElement = null;
this.lastElement = null;
}
/**
* Fuegt ein neues Paket ans Ende der Warteschlange an.
*
* @param data Das neue Paket
*/
public void addDataPacket(DataPacket data){
PacketQueueElement newElement = new PacketQueueElement(data);
if(firstElement != null){
lastElement.setNextElement(newElement);
lastElement = newElement;
}
else{
firstElement = newElement;
lastElement = newElement;
}
}
/**
* Entfernt das erste Element der Warteschlange und gibt es zurueck.
*
* @return Das erste Element in der Warteschlange
*/
public PacketQueueElement getAndRemoveFirstElement(){
PacketQueueElement element = this.firstElement;
this.firstElement = element.getNextElement();
return element;
}
/**
* Gibt das erste Paket aus dem ersten Element zurueck.
*
* @return Das erste Paket
*/
public DataPacket getFirstDataPacket(){
return this.firstElement.getData();
}
/**
* Entfernt das erste Paket der Warteschlange und gibt es zurueck.
*
* @return Das erste Paket in der Warteschlange
*/
public DataPacket getAndRemoveFirstDataPacket(){
return this.getAndRemoveFirstElement().getData();
}
/**
* Gibt das erste Element der Warteschlange zurueck
*
* @return Das erste Element
*/
public PacketQueueElement getFirstElement(){
return this.firstElement;
}
/**
* Ueberprueft, ob die Wartschlange leer ist.
*
* @return true, wenn sie leer ist
*/
public boolean isEmpty(){
if(firstElement == null){
return true;
}
else{
return false;
}
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString(){
PacketQueueElement element = this.firstElement;
String s = "";
while(element != null){
s += element + "\n";
element = element.getNextElement();
}
return s;
}
}
提前谢谢
推荐答案
这混淆了变量和对象 , newElement
确实是一个局部变量,它在方法结束后丢失,但引用指向对象。如果没有引用(变量)指向它,则对象可以进行垃圾回收。在这种情况下,暂时 newElement
和 firstElement
都指向它。当方法退出但 firstElement
仍指向它时,它会丢失 newElement
, lastElement
所以它不能用于垃圾收集。
This is mixing up variables and objects, newElement
is indeed a local variable and it is lost after the method ends, but the reference points to an object. An object is eligable for garbage collection if no references (variables) point to it. In this case temporarily newElement
and firstElement
both pointed to it. It lost newElement
when the method exited but firstElement
still points to it, as does lastElement
so it isn't eligable for garbage collection.
或换句话说:变量是指一个对象,它不是对象本身。
Or in other words: A variable refers to an object, it is not the object itself.
- 变量:你可以写一张纸的地址on
- object:house
- 垃圾收集器:拆除人员
我盖了一幢房子并在一张纸上写下了地址,这样你就可以到达那里,我把那张纸扔给你,你把那张纸上的地址写到你的地址簿中,然后扔掉了废纸。
I build a house and write it's address on a scrap of paper so you can get there, I pass that scrap of paper to you, you write the address from the scrap of paper into your address book, you throw away the scrap of paper.
强拆人员通过查看是否仍有人持有其地址来检查是否有人仍在使用房屋。即使你扔掉了那张纸,你仍然在你的地址簿里有地址,所以房子仍在使用而且没有被拆除
The demolitions crew checks whether anyone is still using a house by seeing if anyone still holds its address. Even though you threw away the scrap of paper you still have the address in your address book so the house is still being used and is not demolished
这篇关于Java中的本地VS全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!