#### Archived

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

# [java] The cost of casting

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

## Recommended Posts

My current project has to cast things, and quite often. I have to cast when I put things into an arraylist and when I take them out. I also have to cast things after the''ve been passed as parameters. How expensive (in terms of time) is this? What alternatives are there? (I suppose I could implement my own arraylist)

##### Share on other sites
Hmm, i can''t tell you in terms of time, but there is some cost, mainly because all casts in Java are made at run-time. *Maybe* you could avoid this expense if you''d use Generics (it''s like the C++ so-called ''templates''). But i think nobody uses generics in java yet (maybe in the future jdk-1.5).

The thing you must think of is: do you really need extreme performance? If not, then don''t worry about it. If yes, well, erm, maybe you can try some other language, such as C++. Or you can try making your own arraylist, yeah...

Victor.

##### Share on other sites
If your only working with a single type of object that would be returned, it would be a good idea to make a class of your own. I had to do this in one project of mine. It has more benifets than just eliminating the overhead of cast as you can eliminate some additional features (syncronized) of the standard classes.

##### Share on other sites
Hi,

The casting is IMO the last thing you should be concerned with regarding to speed. Excessive string concatenation, abuse of the reflection API, unnecessary object creation can be sources of much greater slowdowns than simple casting!

P.S.: The standard collection classes ARE NOT synchronized. In fact only the two legacy collection classes (Vector and Hashtable) are synchronized.
So if you are unable to switch to the new (they're pretty old actually -> since 1.2) collections - Lists (LinkedList, ArrayList), Sets (TreeSet, HashSet) and Maps (HashMap) - don't complain, that the old implementations are synchronized.

have fun

[edited by - Petr Stedry on March 18, 2003 11:28:28 AM]

##### Share on other sites
Judging by the above advice, I suppose that my Vector2D class was a bit of a bad move.
Nearly every method returns a new Vector2D, so I can things like:

unfortunately, I often just need:

which would create three new vectors.

Well, you can see my XXX rated asteroids cloner that I'm working on at the mo. It's seems fast enough, and I haven't optimised anything yet.

[edited by - fractionMan on March 18, 2003 1:13:26 PM]

##### Share on other sites
Ya, immutable classes are great but they create a lot of garbage...

##### Share on other sites
Hi fractionMan,

if your "Vector2D" class consist of two primitive numbers only, you need not worry (IMO) about excessive object creation regarding to Vector2D. Of course if the slowdowns caused by the GC are noticable on the target system you'll have to optimize.

trial and error is a way to go ... sometimes

[edited by - Petr Stedry on March 19, 2003 7:57:44 AM]

##### Share on other sites
quote:
Original post by fractionMan
Nearly every method returns a new Vector2D, so I can things like:

unfortunately, I often just need:

which would create three new vectors.

That does not create three new vectors. And in fact, you should be able to just do this:

vv.add(aVec).sub(aVec).unit();

Give it a try, I think it''ll make things a bit easier for you :D

The only time you''re creating a new object is when you actually use the
new
keyword.

##### Share on other sites
I presume, he uses "new" to create a new object in each of the methods (add,sub). I find this approach good, because it eliminates the possibility, that you link some object accidentally to somewhere, where it should not be as this can produce unexpected behaviour.

##### Share on other sites
-->markuskidd
Petr got it spot on,
In order to be able to stick two methods (or more) in a row, each method must return a Vector2D.
If add() returned a void, then the .sub() would have no meaning.
Example method:

return new Vector2D(x+v.x,y+v.y);
}

Which is what my code does. However, this is so useful I think it''s worth keeping.

On another note, I just found out that arrays of primitive data types are actually objects, so:
int [] x = {1,2,3,4};
int [] y = x;
// y now points to x.
I could understand it if it was Objects, but didn''t expect it with primitives. Took me a while to figure out what was going wring there.

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631428
• Total Posts
3000027
×