简体   繁体   English

传递Java对象的最有效方法?

[英]Most efficient way to pass a Java object around?

I'm considering creating the following method: 我正在考虑创建以下方法:

public static MyBigCollection doSomeStuff(MyBigCollection m) { ... return m; }

and then in another method (perhaps in another class), using it like so: 然后在另一个方法中(也许在另一个类中),像这样使用它:

MyBigCollection mbc = new MyBigCollection();
mbc = stuffClass.doSomeStuff(mbc);

Am I going about this the right way -- is this an efficient way to "do some stuff" to an object? 我是否要采用正确的方法-这是对对象“做些事”的有效方法吗? I'd like to break off the stuff like so for extensibility. 为了扩展性,我想像这样折断。 I've been doing c# for so long I'm just not sure with java. 我一直在做c#这么久了,我不确定使用java。 In c# the method could return void and I can simply call doSomeStuff(mbc) -- which would effectively pass my object by reference and do some stuff to it. 在c#中,该方法可以返回void,而我可以简单地调用doSomeStuff(mbc)-这将有效地通过引用传递我的对象并对它进行处理。 I've been reading that java works differently so I wanted to check with the experts here. 我一直在阅读Java的工作方式有所不同,因此我想在这里与专家核实。

I'd refactor to: 我会重构为:

stuffClass.doSomeStuff(mbc);

(ie, a method that modifies mbc and returns void) (即修改mbc并返回void的方法)

Keep in mind that all Java Objects are stored in heap memory, and passed around with reference pointers. 请记住,所有Java对象都存储在堆内存中,并与引用指针一起传递。

The way you're doing it is fine, but you don't actually need to return the object at the end of the method. 这样做的方式很好,但是实际上不需要在方法末尾return对象。 As such, the following would be simpler... 因此,以下操作会更简单...

MyBigCollection mbc = new MyBigCollection();
stuffClass.doSomeStuff(mbc);

Objects in Java are passed by reference, so any modification on the mbc Object in the doSomeStuff() method would still be retained in the mbc variable after the end of the method call. Java中的对象是通过引用传递的,因此在方法调用结束后,对doSomeStuff()方法中mbc对象的任何修改仍将保留在mbc变量中。

The only reason why you might consider return ing the mbc Object is if you want the ability to join multiple methods together, such as this... 您可能考虑return mbc对象的唯一原因是,如果您希望能够将多个方法结合在一起,例如...

MyBigCollection mbc = new MyBigCollection();
mbc.doStuff1().doStuff2().doStuff3();

In this case, because mbc is returned by each of the doStuff() methods, the next method can be called straight back on to the same Object. 在这种情况下,由于mbc由每个doStuff()方法返回,因此可以直接将下一个方法直接调用到同一Object。 Without return ing the reference, you'd have to do something like this instead... 在不return参考的情况下,您必须要做类似的事情...

MyBigCollection mbc = new MyBigCollection();
mbc.doStuff1();
mbc.doStuff2();
mbc.doStuff3();

Which is the same thing, but not quite as compact. 这是同一件事,但并不那么紧凑。 How you go about it really depends on how you intend to use the methods and the Object itself. 实际情况取决于您打算如何使用方法和对象本身。

There's only one way to pass Java objects around. 只有一种方法可以传递Java对象。 Java passes everything by value. Java通过价值传递一切 Objects aren't passed; 对象不被传递; they live on the heap. 他们生活在堆上。 You pass references around, not objects. 您传递引用,而不是对象。

Same as C#, as far as I know. 据我所知,与C#相同。

This kind of micro-optimization is usually meaningless. 这种微优化通常是没有意义的。

I think your question is around how Java passes references to objects. 我认为您的问题是Java如何将引用传递给对象。 Java passes by value, which can be confusing when first said. Java按值传递,这在第一次说时会造成混淆。 For objects, this means that the value of the reference to the object is passed to the method. 对于对象,这意味着将对对象的引用的值传递给该方法。 Interacting with the object referred to by the value will alter the object 'passed in', so you don't need to return it. 与该值所引用的对象进行交互将更改“传入”的对象,因此您无需返回它。

Strings are treated differently as they are immutable. 字符串是不可变的,因此要区别对待。 Primitives are also pass by value, but as the value passed is not a reference, you will not alter the original variable. 基元也按值传递,但是由于传递的值不是引用,因此您不会更改原始变量。

The easiest way to test this is to write some code and observe (you might also consider the Java tutorials) 测试此问题的最简单方法是编写一些代码并观察(您可能还会考虑Java教程)

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

相关问题 查看ArrayList是否包含Java中的对象的最有效方法 - Most efficient way to see if an ArrayList contains an object in Java 在Java对象中获取项目子集的最有效方法是什么? - What is the most efficient way to get a subset of items in a Java Object? Java中将文本数据作为Image对象处理的最有效方法? - Most efficient way in Java to process text data as an Image object? 用Java创建日期对象的最有效方法是什么 - What is the most efficient way to create Date Object in Java 从 java 列表 object 创建 XML 的最有效方法 - Most Efficient way to create XML from java List object 将无符号char *从C ++作为字节[]传递给Java的最有效方法 - Most efficient way to pass unsigned char* from C++ to java as byte[] 将Java套接字文件描述符传递给C二进制文件的最有效方法 - Most efficient way to pass Java socket file descriptor to C binary file 连接两个对象数组的最有效方法 - Most efficient way to join two object arrays 在Java中使用动作侦听器的最有效方法? - Most efficient way of using an action listener in Java? 用Java调用外部Web服务的最有效方法? - Most Efficient Way of calling an external webservice in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM