Does type-cast impose overhead at runtime ? - Programmers Heaven

Howdy, Stranger!

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

Categories

Does type-cast impose overhead at runtime ?

ninchagoraninchagora Posts: 82Member
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

  • moomoo Posts: 147Member
    : 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.