• Advertisement


This topic is now archived and is closed to further replies.

[java] Casting

This topic is 5615 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m building a simple (they all start off simple, don''t they) game engine for a final project at my school. I''m implementing a resource manager for the engine. The way I''m implementing it is the have a base abstract class Node which has other classes(AudioNode, ImageNode) devrived from it. I have a linked list that is used to muve through the data. I''m using the Node class as the class to pint to the next node which makes it that I haveto cast all the nodes into their proper type when I want to use them. THis all is working, but I recently read that preforming casts is an expensive operation. What my question is, is should I keep the current method, make seperate linked lists for all the different classes, or something else?

Share this post

Link to post
Share on other sites

Without more background I can not help you much.

Why did you make a design like this ?
What does the Node class definition contain ? What''s common among those nodes (Audio, Image ...) that you put it into one base class for all those parts ?

And about performing casts ....

If you don''t need to have all those Nodes in one list, don''t do that - you prevent all that casts in case they would prove costly (but I really doubt it - IMHO a cast can be done in constant time).

But always make the program work first and optimize it afterwards!

If you''re looking for ways to optimize your code, then ask yourself:
  • am I using the appropriate containers (LinkedLists retain order, ArrayLists for indexed element retrieval, HashMaps give key-value mappings, TreeSets keep elements ordered)
  • are there not many superfluous objects created ?
  • do I use buffered I/O operations?
  • et cetera, et cetera ...

That will have to do for the info you provided.

have a nice day/night (depending on your timezone)

Petr Stedry

Share this post

Link to post
Share on other sites

  • Advertisement