1

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. enter image description here

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.

Community
  • 1
  • 1
Razvi
  • 400
  • 4
  • 17
  • 3
    It's totally not about performance, it's about torpedoing the ability of the compiler to enforce type safety – Nathan Hughes May 07 '17 at 20:32
  • I would suggest you to look read about JMH benchmarking. That will help u to see yourself what is better and what not. – Sneh May 07 '17 at 20:33
  • 6
    Why not use generics? `Iterator iterator = documentos.createIterator();` – Hugues M. May 07 '17 at 20:34
  • 1
    I'm here to learn what other people have to say but I don't understand why my question is getting downvoted. I will have a further read about the compiler and enforcing type safety. It is also a good idea to use generics – Razvi May 07 '17 at 20:36
  • 1
    This isn't the kind of thing you should be thinking about, even if you were interested in learning how to write optimized code. Whether you explicitly declare a local variable will make no discernible difference. In fact, the JVM has to put the return value of `currentElement()` somewhere, whether you declare a variable for it or not. – Radiodef May 07 '17 at 20:51
  • Understood. Thank you sir! – Razvi May 07 '17 at 21:05
  • 2
    As already said, you definitely should use generics. They save you the casting.... though there will be an implicit cast. With respect to speed, neither the local variable nor using generics is of any importance (all variants may or may not lead to identical code; if not, then there may be minor random speed differences). +++ However, using makes you code much more readable. A readable code is much easier to optimize *when needed*. – maaartinus May 08 '17 at 07:18
  • I've re-read the title. Instantiating a new object is usually a bit costly, but there's no new instance in your code. The local variable is just a reference to an object, – maaartinus May 08 '17 at 07:25
  • Understood @maaartinus. As already stated by @Hugues, I''m probably switching my code. It's a good idea to have a `public getElement(int)` and then, when instanciating, `Documento documentos = new Documentos();` Thank you all :) **UPDATE**: You are right. I'm just creating a named pointer, or reference. I'll edit the question. – Razvi May 08 '17 at 07:26

0 Answers0