Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Cost of Casting?




On Apr 26, 2005, at 6:27 PM, Satoru Moriwaki wrote:

Hi,

How much does casting really affect the performance
(time and space) in Java?

I recall something like 5 for time and 9 for space (or I could have those backwards).


I've read a few articles on the net and most of them
said that the casting is really cheap in most cases.

Well it isn't a NOP by any means (it has to check to insure the cast requested is safe / allowed and throw a cast exception if not) but it is likely one of critically / heavily optimized aspects of a JVM / JIT.


I use Apple's WebObjects and many classes in WebObjects
return an Object type and casting is often necessary.

Don't look now but all of Java's collection classes return and take Object as well.


In other words it is a reality of Java, don't worry about it unless when profiling you see a performance issue that can alleviated by avoiding it (not saying you should go out cast like mad for the fun of it).

Note Java 5 (aka 1.5) has generics [1] to help reduce the overhead of typing all those casts in your code (assuming you like its syntax, personally I am still trying to like it... really really I am). Also related is autoboxing [2] for the pesky primitive types.

-Shawn

[1] <http://java.sun.com/developer/technicalArticles/J2SE/generics/>
[2] <http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html>

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden
References: 
 >Cost of Casting? (From: Satoru Moriwaki <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.