Is Java application performance dependent on passing of variables to method? -
maybe trivial question experienced programmers wonder if there significant performance difference (with big or big amount of data in collection) between 2 difference approaches of passing variables?
i've made tests rather small data structures , don't see significant differences. additionally not sure if these differences aren't caused interferences other applications run in background.
class collection:
public class testcollection { arraylist<string[]> mytestcollection = new arraylist<string[]>(); public testcollection() { fillcollection(); } private void fillcollection() { // here fillng big amount of data } public arraylist<string[]> geti() { return mytestcollection; } }
and methods 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) { (int = 0; < tc.geti().size(); i++) { // actions collection using tc.geti().dosomething } } public void approach_2(arraylist<string[]> t) { (int = 0; < t.size(); i++) { // actions collection using t.dosomething } } }
regards.
no, there no real difference here.
java passes object references
methods, not copies of entire object. similar pass reference
concept in other languages (although passing object reference called method, passed value).
if come c programming background it's important understand this!
and, tips - firstly, it's better practise declare list list<...>
rather arraylist<...>
, this:
list<string[]> mytestcollection = new arraylist<string[]>();
and secondly, can use improved for
loop on lists, this:
// first case (string[] s : tc.geti()) { /* */ } // second case (string[] s : t) { /* */ }
hope helps :)
Comments
Post a Comment