Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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.