Does type-cast impose overhead at runtime ?

Does cast require additional operations at runtime, or is it just a trick for the compiler ?


For example:

Is this

[code]
MyClass obj = new MyClass(...);
obj.myField = ... ;
obj.myMethod(...);
[/code]

faster than this:

[code]
Object obj = new MyClass(...);
((MyClass)obj).myField = ... ;
((MyClass)obj).myMethod(...);
[/code]

?

Thanks for your consideration

Comments

  • : Does cast require additional operations at runtime, or is it just a trick for the compiler ?
    :
    :
    : For example:
    :
    : Is this
    :
    : [code]
    : MyClass obj = new MyClass(...);
    : obj.myField = ... ;
    : obj.myMethod(...);
    : [/code]
    :
    : faster than this:
    :
    : [code]
    : Object obj = new MyClass(...);
    : ((MyClass)obj).myField = ... ;
    : ((MyClass)obj).myMethod(...);
    : [/code]
    :
    : ?
    :
    : Thanks for your consideration
    :

    Hi,
    there is definately an overhead for this cast, since java at least has to check whether the cast is all right (ClassCastException).

    greetings mo
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion