Java应用程序的性能是否依赖于向方法传递变量?
Java应用程序的性能是否依赖于向方法传递变量?,java,performance,methods,collections,Java,Performance,Methods,Collections,对于有经验的程序员来说,这可能是一个微不足道的问题,但我想知道在传递变量的两种不同方法之间是否存在显著的性能差异(在收集大量数据的情况下? 我做了一些测试,但是数据结构非常小,我没有看到任何显著的差异。此外,我不确定这些差异是否是由后台运行的其他应用程序的干扰造成的。 带集合的类: public class TestCollection { ArrayList<String[]> myTestCollection = new ArrayList<String[]>
对于有经验的程序员来说,这可能是一个微不足道的问题,但我想知道在传递变量的两种不同方法之间是否存在显著的性能差异(在收集大量数据的情况下?
我做了一些测试,但是数据结构非常小,我没有看到任何显著的差异。此外,我不确定这些差异是否是由后台运行的其他应用程序的干扰造成的。
带集合的类:
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;
}
}
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
}
}
}
公共类TestCollection
{
ArrayList myTestCollection=新建ArrayList();
公共测试集合()
{
fillCollection();
}
私有集合()
{
//这里有大量的数据
}
公共数组列表getI()
{
返回myTestCollection;
}
}
以及对收集进行操作的方法:
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;
}
}
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
}
}
}
公共类测试
{
静态TestCollection tc=newtestcollection();
公共静态void main(字符串[]args)
{
新试验().进近_1(tc);
新测试().方法2(tc.getI());
}
公共无效方法_1(测试集合t)
{
对于(int i=0;i
问候。不,这里没有真正的区别 Java将对象
引用传递给方法,而不是整个对象的副本。这类似于其他语言中的passbyreference
概念(尽管我们实际上是通过值传递一个对象引用到被调用的方法)
如果你有C编程背景,了解这一点很重要
还有一些提示-首先,最好将列表声明为list
,而不是ArrayList
,如下所示:
List<String[]> myTestCollection = new ArrayList<String[]>();
// first case
for (String[] s : tc.getI()) { /* do something */ }
// second case
for (String[] s : t) { /* do something */ }
希望这有帮助:)不,这两种方法都只是将引用的值复制到堆栈,所以它是相同的。一个数组引用或ArrayList引用都是相等的。我知道当我传递给方法“ArrayList t”时,它被复制到堆栈中。这对我来说很清楚。但当我传递“TestCollection t”时,它意味着复制了包含所有变量的整个类?这意味着它会消耗大量内存,内存中充满了我不需要的不必要的数据。不,不是整个数组,只是复制了对数组的引用。好的。但若传递了引用,则意味着在传递的方法集合结束后应该修改。我希望您知道java是按值传递的,也就是说,您对集合所做的所有操作都会影响原始集合或数组。只有您不能更改引用,因为它是按值传递的。实际上,Java从不按引用传递。更改方法参数的值不会影响调用方看到的内容。对于非基本类型,它是一个对象引用,通过值传递。这与passby-reference的效果差不多,但它不是在C pass-by-reference的意义上,我实际上可以编写一个方法来更改参数对调用方引用的对象。好的,谢谢Sean的澄清。我试着用C语言的思维来解释它。我会修改的。Java通过引用传递!!!我认为我们的结论是java实际上是按值传递对象引用的。@NarendraPathai这个问题的每个答案都说它是按值传递的,这是正确的。