This question might be related with this other one How much processing and memory use does casting take in Java?
I know when it comes to Java optimization is not a very discussed subject. I'm also aware that it's more important to write readable and maintenable code rather than writing sketchy, one-liner code. I agree when they say "you should not care about costs of casts" and in fact I'm only caring about writing code that follows important principles as SOLID, KISS, DRY, I care a lot about testing, YAGNI, etc... but this question came to my mind in my spare time when I was thinking about the code itself and I'm not able to answer myself.
The thing is right now I'm using a self implementation of Iterator pattern and I'm combining it with Aggregate interfaces in order to achieve polymorphism.
Here is a brief UML I did with ObjectAid.
As you can see I have Iterator
interface whose concrete implementation or instantiation is ListIterator
.
On the other hand I have Aggregate
interface which is implemented by all the aggregate classes I have. In the example only Documentos
is shown.
So the thing is, when another class whose aggregate is Documentos will call from his Documentos attribute something like Iterator iterator = documentos.createIterator();
it will obtain an ListIterator object which is an Iterator. Then, when it comes to work with the collection of items that Documentos is in charge of (that's why Documentos is an aggregate and that's why it exists, it's his purpose) I would do something like iterator.firstElement(); iterator.currentElement();
.
As you can see, currentElement() from Iterator interface is returning an Object object since what is really happening is that ListIterator has an Aggregate atribute, or field, and what is delegating the "currentElement()" as follows:
//In ListIterator:
public Object currentElement(){
aggregate.getElement(current);
}
and
//In Documentos
public Object getElement(int){
repository.findById(idDocumentos.get(int));
}
As you can imagine, the repository is not instantiating pure Object class objects but, let's call it, DocumentoDomain.
So my question is: I will have to work with these methods a lot and I will have to deal with Object -> DocumentoDomain casting all the time. Which of the following forms of doing so is more optimal?
Let's say I must retrieve the name of the DocumentoDomain. I could, from inside a class with a Documentos attribute:
Iterator iterator = documentos.createIterator();
iterator.firstElement();
DocumentoDomain doc = (DocumentoDomain) iterator.currentElement();
String docName = doc.getName();
or
Iterator iterator = documentos.createIterator();
iterator.firstElement();
String docName = ((DocumentoDomain) iterator.currentElement()).getName();
How does this impact the overall resource consupmtion? Does it really matter or it depends only on wheter I'm going to use that "element" later or not?
UPDATE: As already stated in the comments, I'm not instanciating any new object. What I'm really doing is creating a reference to an alraedy existing one. This right here:
DocumentoDomain doc = (DocumentoDomain) iterator.currentElement();
is not instantiation.