简体   繁体   English

Java的行为就像我有指针一样。 为什么?

[英]Java is acting like I've got pointers. Why?

I'm building an application using java.swing. 我正在使用java.swing构建应用程序。 I've got a main panel, a grid panel (which displays squares), and a details panel (which brings up the details on each square and lets you edit them). 我有一个主面板,一个网格面板(显示正方形)和一个详细信息面板(显示每个正方形的详细信息并让您对其进行编辑)。 The squares are also objects. 正方形也是对象。

I have the main panel listening for mouse clicks, and if you click on the grid panel, the grid panel will go through it's ArrayList of squares and find which square you clicked on, returning it. 我的主面板监听鼠标单击,如果您单击网格面板,则网格面板将遍历正方形的ArrayList并找到您单击的正方形,然后将其返回。 This square is passed by the main panel to the display panel, which then sets it's local variable currentSquare equal to the square being passed in. 该正方形由主面板传递给显示面板,然后显示面板将其局部变量currentSquare设置为等于要传递的正方形。

Where I'm getting confused is that I can edit currentSquare, and the corresponding square in the ArrayList is being edited! 让我感到困惑的是,我可以编辑currentSquare,并且正在编辑ArrayList中的相应正方形! I can change the currentSquare's public variables or use a setter to change them, but the effect is the same regardless. 我可以更改currentSquare的公共变量,也可以使用setter来更改它们,但是无论如何效果都是一样的。 While this is the effect that I'm wanting to have happen, I was under the impression that I would have to have some sort of getters/setters to carry the edited currentSquare back to the ArrayList and replace the old square. 虽然这是我希望发生的效果,但我的印象是,我将不得不使用某种吸气剂/吸气剂,才能将编辑后的currentSquare带回到ArrayList并替换旧的正方形。

My current theory as to why this is happening is that it has something to do with Java's references, since the square is a custom Object, but I'm not certain that this is the case or what caused this. 我目前关于发生这种情况的原因的理论是,它与Java的引用有关,因为正方形是自定义对象,但是我不确定是这种情况还是造成这种情况的原因。 Ultimately I want to know what I did to cause this situation and if it's good/ok programming practice to leave this as it is or build on this (and if I do build on it working like this, how likely is it that it will break). 最终,我想知道导致这种情况的原因,以及是否按原样保留它或在此基础上进行构建是一种好/好的编程实践(如果我以此为基础进行构建,那么它破裂的可能性有多大)。

In Java, when you call a method with an object, you're not sending a copy of the object, but rather a reference to it. 在Java中,当您使用对象调用方法时,您不是在发送对象的副本,而是发送对它的引用。 Both the caller and the calleee will refer to the same object and the object will be eligible for garbage collection once all references to it disappear. 调用方和被调用方都将引用同一对象,并且一旦对该对象的所有引用都消失,该对象将有资格进行垃圾回收。

Java is called "pass-by-value" because the references themselves are values, just like the primitives are values. Java之所以称为“传递值”,是因为引用本身就是值,就像基元就是值一样。 When you're calling a method with an object, you're sending the address of the object as a parameter, not a copy of the object. 当使用对象调用方法时,您将对象的地址作为参数发送,而不是对象的副本。 You don't have access to the value of the address, you can only access the object referred to by it. 您无权访问该地址的值,只能访问该地址所引用的对象。 Example: 例:

import java.util.*;

class Test { 
    private static void test(Map<String, String> a, int b) {
        a.put("test", "def");
        b = 10; 
    }

    public static void main(String[] args) {
        Map<String, String> a = new HashMap<String, String>();
        a.put("test", "abc");

        int b = 5;

        test(a, b); 

        System.out.println(a);
        System.out.println(b);
    }   
}
{test=def}
5

The above compiles into something like: 上面的内容编译为:

test(0x123456, 5);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当两个监视器字段引用同一个对象时,为什么两个同步块表现得像我提供了不同的监视器对象? - Why are two synchronized blocks acting like I've provided different monitor objects, when both monitor fields reference the same object? 我在@jdbc java中有@PO而不是我的参数字符串 - I've got @PO instead of my parameter string in jdbc java 我有无限循环 - I've got infinite looping 我正在尝试至少对数组进行排序。 现在我有这样的事情: - I'm trying to sort an array at least at greatest. Now I've got something just like this: Java-我有一个库(.a / .so)和标头(.h),如何使用Java调用它们的函数? - Java - I've got a library(.a/.so) and header(.h), how to use Java call their functions? I've got java.lang.OutOfMemoryError: Java heap space testing ActiveMQ by JMeter on Linux build agent - I've got java.lang.OutOfMemoryError: Java heap space testing ActiveMQ by JMeter on Linux build agent 如果我有两个Java类(A和B),如何确保A在B之前先编译? - If I've got two java classes(A and B), how do I ensure that A compiles for sure before B does? 为什么Java对象指向指针? - Why are Java objects pointers to pointers? 当它不是 Java 时,变量就像指针一样 - variable acting like pointer when its not Java 为什么bottomMargin / topMargin / rightMargin的行为就像leftMargin? - Why bottomMargin/topMargin/rightMargin is acting like leftMargin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM