Hint: You're calling upon two objects when not referencing the same interface.
No you're not. In both cases its a call via a vtable to a method. No difference.
Did you run the profiling like I suggested?
Hint: You're calling upon two objects when not referencing the same interface.
Nope, don't have Java on this box. But your test is flawed regardless. You are still calling upon two objects .. the interface, and the object. "object" in my previous reply not being the programmatic definition of instantiated object, but "thing". Even on the vtable this makes a difference. When it references the interface on the object, verifying the interface is satisfied.
Your test is flawed, regardless. Compile two applications, one with List, one with ArrayList. Then compare. Not one with both.
and irrespective of this digression, why use "List" and not "ArrayList" when you are expecting an ArrayList, not a polymorphic "List," object?
"object" in my previous reply not being the programmatic definition of instantiated object, but "thing".
Can you not read?
and not once have I said two objects are being created.
You still can't read.. your test is flawed.
Having to call the interface of a parent class/Interface increases memory usage when it is not necessary, over calling the specific interface of the object you will be instantiating.
Your test is flawed, regardless. Compile two applications, one with List, one with ArrayList. Then compare. Not one with both.
Perhaps you'd like to post a counterexample that I can profile that shows your case?
public class Main {
public static void main(String[] args){
{
// Comment out one of the following lines, depending on whether you wish to test ArrayList or List
//ArrayList reference = new ArrayList();
List reference = new ArrayList();
long start = System.currentTimeMillis();
for (int i = 0 ; i < 10000; i++) {
ListIterator iter = reference.listIterator();
System.out.println(iter);
}
long end = System.currentTimeMillis();
System.out.println(end - start) ;
}
}
}
Er, he is creating two objects.and not once have I said two objects are being created
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class Main {
public static void main(String[] args){
{
// Comment out one of the following lines, depending on whether you wish to test ArrayList or List
ArrayList reference = new ArrayList();
//List reference = new ArrayList();
long start = System.currentTimeMillis();
for (int i = 0 ; i < 10000; i++) {
ListIterator iter = reference.listIterator();
System.out.println(iter);
}
long end = System.currentTimeMillis();
System.out.println(end - start) ;
}
}
}
I'm getting 454, 455, 441 for List, 422, 433, 418 for ArrayList.. (both run 3 times.)
public class Main {
public static void main(String[] args){
{
ArrayList reference = new ArrayList();
//List reference = new ArrayList();
for (int i = 0 ; i < 10000; i++) {
reference.add(new Object());
}
long start = System.currentTimeMillis();
for (int i = 0 ; i < 10000; i++) {
System.out.println(reference.size());
}
long end = System.currentTimeMillis();
System.out.println(end - start) ;
}
}
}
In the context of this problem using an ArrayList does not bring any particular benefits. Seeing as ArrayLists are in fact built on arrays mastering the underlying mechanisms first will ultimately give a far deeper knowledge of the language than a black box approach.I apologise in advance if this appear condescending, I do not intend it to be so.
Surely having the initiative to learn for your self what an ArrayList is, is not beyond most people?!
That's fair enough, I can accept that. I did overlook the need to learn what's under the bonnet, so to speak.In the context of this problem using an ArrayList does not bring any particular benefits. Seeing as ArrayLists are in fact built on arrays mastering the underlying mechanisms first will ultimately give a far deeper knowledge of the language than a black box approach.
No offence intended to Shicky here, I thought he was having enough trouble grasping the basics without adding Collections to the mix.
Maybe I was pitching too low. I don't know.
As for the performance argument I'll only make one comment. Even if your argument were correct optimising your printMarks() method to buffer the output rather than make 2 io calls per loop would provide overwelming gains over avoiding Interfaces.
I'm still getting lower results for using ArrayList directly, rather than List. ~318 for ArrayList, ~350 for List.Thats because the bulk of the processing is involved in the output. Thats why ArrayList, which generates a longer toString output than List, on account of its longer name, has larger times.
try this:
Code:public class Main { public static void main(String[] args){ { ArrayList reference = new ArrayList(); //List reference = new ArrayList(); for (int i = 0 ; i < 10000; i++) { reference.add(new Object()); } long start = System.currentTimeMillis(); for (int i = 0 ; i < 10000; i++) { System.out.println(reference.size()); } long end = System.currentTimeMillis(); System.out.println(end - start) ; } } }
That code has the same output for both cases, and shows no difference.
I'm still getting lower results for using ArrayList directly, rather than List. ~318 for ArrayList, ~350 for List.
public void printMarks () {
String out = "";
for (int i = 0; i < marksArray.size()-1; i++) {
out += marksArray.get(i)+", ";
}
out += marksArray.get(marksArray.size()-1);
System.out.println(out);
}
Yes.And you're using optimised compilation (the -O option to javac)?