1


Maybe this is trivial question for experienced programmers but i wonder if there is any significant performance difference (with big or very big amount of data in collection) between two difference approaches of passing variables?
I've made a tests but with rather small data structures and i don't see any significant differences. Additionally i am not sure if these differences aren't caused by interferences from other applications run in background.

Class with collection:

public class TestCollection
{
    ArrayList<String[]> myTestCollection = new ArrayList<String[]>();
    public TestCollection()
    {
      fillCollection();
    }

    private void fillCollection()
    {
        // here is fillng with big amount of data
    }

    public ArrayList<String[]> getI()
    {
        return myTestCollection;
    }
}

And methods that operate on collection:

public class Test
{
    static TestCollection tc = new TestCollection();

    public static void main(String[] args)
    {
        new Test().approach_1(tc);
        new Test().approach_2(tc.getI());
    }

    public void approach_1(TestCollection t)
    {
        for (int i = 0; i < tc.getI().size(); i++)
        {
            // some actions with collection using tc.getI().DOSOMETHING
        }
    }

    public void approach_2(ArrayList<String[]> t)
    {
        for (int i = 0; i < t.size(); i++)
        {
            // some actions with collection using t.DOSOMETHING
        }
    }
}

Regards.

rainbow
  • 1,161
  • 3
  • 14
  • 29
  • No both the approaches are just copying the value of reference to stack and so it will be same. An array reference or ArrayList reference both will be equal AFAIK. – Narendra Pathai Sep 04 '13 at 09:24
  • I understand when i pass to method "ArrayList t" it is copied to stack. This is clear for me. But when i pass "TestCollection t" it means whole class with all variables is copied? It means it can consume a lot of memory filled with unnecessary data which i don't need. – rainbow Sep 04 '13 at 09:32
  • No not whole array just the reference to the array is copied. – Narendra Pathai Sep 04 '13 at 09:33
  • Ok. But if reference is passed, it means after end of method collection which was passed should be modified. And it is not true. – rainbow Sep 04 '13 at 09:40
  • I hope you know java is pass by value, that said all the operations you do on the collection will be affect the original collection or array. Only you can't change the reference as it is pass by value. – Narendra Pathai Sep 04 '13 at 09:42
  • Until you have a performance metric which suggests you haven a difference, you should assume you don't. Even after you have a measure, you would be very sceptical that you result is meaningful. Trying to guess what matters to performance is usually a waste of your time. – Peter Lawrey Sep 04 '13 at 09:42
  • On the note of @PeterLawrey you can perform micro benchmarking and see if it really has some difference http://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java – Narendra Pathai Sep 04 '13 at 09:44
  • Narenda you are right. I muddled up passing reference with some other facts from my application. – rainbow Sep 04 '13 at 09:55

1 Answers1

3

No, there is no real difference here.

Java passes object references to methods, not copies of the entire object. This is similar to the pass by reference concept in other languages (although we are actually passing an object reference to the called method, passed by value).

If you come from a C programming background it's important to understand this!

And, some tips - firstly, it's better practise to declare your list as List<...> rather than ArrayList<...>, like this:

List<String[]> myTestCollection = new ArrayList<String[]>();

And secondly, you can use the improved for loop on lists, like this:

// first case
for (String[] s : tc.getI()) { /* do something */ }

// second case
for (String[] s : t) { /* do something */ }

Hope this helps :)

vikingsteve
  • 38,481
  • 23
  • 112
  • 156
  • 1
    Actually, Java *never* passes by reference. Changing a method argument's value never affects what the caller sees. For non-primitive types, it's an object reference, passed by value. This has much the same effect as pass-by-reference but it is not in the sense of C pass by reference, where I could actually write a method that changed what object an argument refers to for the caller. – Sean Owen Sep 04 '13 at 09:39
  • 1
    Ok, thanks for the clarification Sean. I was trying to explain it from a C-language mindset. I'll amend. – vikingsteve Sep 04 '13 at 09:41
  • Java passes by reference!!! http://stackoverflow.com/questions/40480/is-java-pass-by-reference – Narendra Pathai Sep 04 '13 at 09:43
  • 1
    I think we concluded that java in fact passes object references by value. – vikingsteve Sep 04 '13 at 09:45
  • @NarendraPathai every answer to that question says it's pass-by-value, which is correct. – Sean Owen Sep 04 '13 at 09:49
  • My intention is to have a List. It is not a mistake. I know benefits of using short for but i just prefer long one. – rainbow Sep 04 '13 at 09:50
  • @user2018059 Ok, no worries, I removed the String[] vs String note from my answer. Does this explanation answer your question now? – vikingsteve Sep 04 '13 at 09:58