简体   繁体   English

在Java中将对象作为参数传递

[英]passing objects as parameters in java

I'm trying to make a 2d game in Java. 我正在尝试用Java制作2D游戏。

In the process I soon noticed that many objects needed bounds (x1, x2, y1, y2) and as a consequence many methods thus had parameters (int x1, int x2, int y1, int y2). 在此过程中,我很快注意到许多对象需要边界(x1,x2,y1,y2),因此许多方法因此具有参数(int x1,int x2,int y1,int y2)。

So I thought what the heck, why not make an object IntRectangle with attributes x1, x2, y1, y2 (which I set as public for a performance boost), along with a few helpful methods like getWidth(), getHeight(), etc. 因此,我想了一下,为什么不使用属性x1,x2,y1,y2(为提高性能而设置为公共属性)以及一些有用的方法(例如getWidth(),getHeight()等)来创建对象IntRectangle 。

At this point my code is much cleaner and I haven't lost much performance since the variables are public. 在这一点上,我的代码更加简洁,由于变量是公共的,因此我并没有损失太多性能。

I soon noticed, unlike passing primitive values into a method, the values of the passed object changed when I changed them in the method. 我很快注意到,与将原始值传递给方法不同,当我在方法中更改传递的对象的值时,传递的对象的值也会更改。

For instance, with a primitive value 例如,使用原始值

 int x = 10;

 subtract5(x);
 print(x);

 void subtract5(int i) {
     i -= 5;
 }

this line of code would print 10. But with an object... 这行代码将显示10。但是有一个对象...

class XY {
    public int X;
    public int Y;
}

main {
    XY xy = new XY();

    xy.X = 5;
    xy.Y = 10;

    changeX(xy);

    print xy.X;   
}

changeX(XY xy2) {
    xy2.X = 7;
}

It prints 7; 打印7;

The theory I've developed is that an int is not a pointer to an int, it's an int stored in the register. 我提出的理论是,int不是指向int的指针,它是存储在寄存器中的int。 It is pushed when a method is called and then popped back when returned, unchanged. 在调用方法时将其推送,然后在返回时弹出,且保持不变。 An object is a reference to an object. 对象是对对象的引用。 The object itself isn't pushed onto the stack, just the pointer. 对象本身不只是指针而被压入堆栈。

But I've tired this with strings (that I know are objects) and they behave just like ints in this matter, which confuses me. 但是我对字符串(我知道是对象)感到厌倦,并且在此问题上它们的行为就像int一样,这使我感到困惑。 Perhaps they make a copy out of themselves somehow? 也许他们以某种方式自己制作了副本?

Well, I hope someone can shed some light on this matter and tell me where my reasoning is taking a wrong turn and hopefully give me some advice on how to clean up my code, keep performance, but without making it venerable. 好吧,我希望有人能对此事有所启发,并告诉我我的推理在哪里转错了,并希望给我一些有关如何清理代码,保持性能的建议,但又不要使它变得令人尊敬。

A object in java is a block of memory in the heap space. Java中的对象是堆空间中的一块内存。 All you pass around are references to this object/memory area. 您传递的所有内容都是对此对象/内存区域的引用。 (The references itself are passed by value) So if you have an Object Rectangle with four ints your methos will change the memory of the heap ara which is references by the passed object reference. (引用本身是通过值传递的)因此,如果您的对象矩形具有四个整数,则您的方法将更改堆ara的内存,该内存是通过传递的对象引用进行引用的。

Strings are immutable - so the object cannot change and every modifier method returns a new instance. 字符串是不可变的-因此对象无法更改,每个修改器方法都返回一个新实例。

See http://javadude.com/articles/passbyvalue.htm for more information. 有关更多信息,请参见http://javadude.com/articles/passbyvalue.htm

The fact is that Java is always pass-by-value. 事实是Java总是按值传递。 In particular Java passes objects as references and those references are passed by value. 特别是Java将对象作为引用传递,而这些引用则按值传递。 In case of primitive type, it is passed directly by value. 如果是基本类型,则按值直接传递。

Further details in the following question: 以下问题的更多详细信息:

Is Java "pass-by-reference" or "pass-by-value"? Java是“按引用传递”还是“按值传递”?

Strings are constant; 字符串是常量; their values cannot be changed after they are created.If you change the value of a String it will create a new String object and return a reference to the new String object ie a new instance is created .Eg 它们的值创建后就无法更改。如果更改String的值,它将创建一个新的String对象,并返回对该新String对象的引用,即创建了一个新实例。

String str="abc";
str=str+"d";

In this case a new String object is created in the String pool and it is now referred by str.The old string is garbage collected. 在这种情况下,将在字符串池中创建一个新的字符串对象,现在将它用str引用。旧字符串将被垃圾回收。

Ok, I might have been a bit vague, but the question was not how java passes variables. 好的,我可能有点含糊,但问题不在于java如何传递变量。 I just came from that thread. 我只是来自那个话题。 I've figured that out, it was just the String class that was confusing me. 我发现,只是String类使我感到困惑。 And the eventual performance gain of having public attributes. 并最终获得具有公共属性的性能。 I'll make another attempt, thanks for all your answers! 我将再尝试一次,谢谢您的回答!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM