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.

8
  • 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. Commented Sep 4, 2013 at 9:24
  • I understand when i pass to method "ArrayList<String[]> 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. Commented Sep 4, 2013 at 9:32
  • No not whole array just the reference to the array is copied. Commented Sep 4, 2013 at 9: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. Commented Sep 4, 2013 at 9: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. Commented Sep 4, 2013 at 9:42

1 Answer 1

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 :)

Sign up to request clarification or add additional context in comments.

7 Comments

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.
Ok, thanks for the clarification Sean. I was trying to explain it from a C-language mindset. I'll amend.
I think we concluded that java in fact passes object references by value.
@NarendraPathai every answer to that question says it's pass-by-value, which is correct.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.