Seriously screwed for Java assessment

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?
 
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?

An interface isnt an object. In both my examples, one object is being created, and one reference to the object.

In the first case, a Test reference is created, and made equal to the address of an object of type TestImpl. In the second case a TestImpl reference is created and made equal to the address of an object of type TestImpl.

Clearly both these processes use the same amount of memory, and take the same time - Java references are all of equal size and complexity - they're pretty much as simple as C++ reference counted pointers.

So onto the method calls.

Call one: You're calling a method on an object via a reference to an implementing interface - you go to the objects method table and call the method that is pointed to.

Call two: You're calling a method on an object via a reference to its type - you go to the objects method table and call the method that is pointed to.

Why do you think there are two objects being created? And if there *are* two objects being created (the derived class and the interface) then what does the creation of the 'interface' involve? How much memory does it take up? What does it consist of?
 
Can you not read?

"object" in my previous reply not being the programmatic definition of instantiated object, but "thing".

and not once have I said two objects are being created - I said called, again read the above quote if having difficulty. I refer to two "things" being called upon. The "Interface" is looked up, or the interface of the class/object is looked up. As we are going to be using the class/object anyway, why bother looking up the "Interface"? If we specify that variable A will be of TypeA, and TypeA is an "Interface" then the value, before it is assigned to the variable, is verified against TypeA. Same for accepting params in a method call.

So, we can either verify that the object is a List, pull up the Interface List, verify messages, then call up the Class ArrayList, or we can just call up ArrayList for both tasks.
 
Last edited:
Can you not read?



and not once have I said two objects are being created.

Why dont you get back to me once you've had a chance to profile the code I posted. If you do that and can show that Method calls via interface references are any way more expensive (in terms of speed or memory usage) than calls via derived class references then I'll happily concede that you are correct.

Until that point you're merely pontificating without evidence.
 
You still can't read.. your test is flawed.

Nope. It is an exact test of your statement:

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.

Call 1 in my example is a call to the interface. Call 2 is a call to the specific type of the object that is instantiated. Both are equal in terms of memory usage/speed. Your statement is wrong.

Unless you have something more concrete than simply saying it is so. I dont deal in people simply saying something is true without evidence, especially when I have hard evidence to the contrary.

Perhaps you'd like to post a counterexample that I can profile that shows your case?
 
you honestly cannot read, then?

Can't you?

Two applications.. one with a List.. another with an ArrayList.. come on diddums, you can do it.
 
Code:
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) ;
        }
    }
}

Whether the ArrayList line or the List line is commented out, both ways gives the same result, time wise (small variations notwithstanding).

If I am testing the wrong thing then please show me some code that demonstrates your point.
 
I'm getting 454, 455, 441 for List, 422, 433, 418 for ArrayList.. (both run 3 times.)

Full code:
Code:
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.)

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 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?!
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.
 
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.
That's fair enough, I can accept that. I did overlook the need to learn what's under the bonnet, so to speak.
 
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.
 
Here is a more efficient printMarks method, it only makes a single IO call and doesn't require a conditional statement to check for the last element. There also is no need for an Iterator.

Another way could be to use an enhanced for loop (for each loop) to do the same but you wouldn't be able to check for the last element efficiently (you could get the last element and then check if the current one is the last element but that is way less efficient). Another approach is to use the enhanced for loop and insert a comma after each element then create a substring of the final string to remove the comma but that is a bit of a kludge and probably no more efficient.


Code:
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);
}
 
Back
Top Bottom