Answer the question
In order to leave comments, you need to log in
What is the performance of cast in Java?
Suppose there is an abstract class that is inherited by some decorators.
In theory, you will need to cast decorated objects among themselves.
How much will it affect the performance of the java program?
Are there any best practices for such cases?
Answer the question
In order to leave comments, you need to log in
best practices in such cases - do not cast, but use polymorphism. After all, decorators are needed to decorate / abstract something, and not to pull out the guts of a specific implementation from them with the help of a cast.
Let's start with the fact that the very formulation of the question is fundamentally wrong. It could sound like this: "I made the code, took measurements and everything rested on the cast operation, what should I do?".
These are written truths, premature optimization is the root of evil!
But if the question itself, then the type conversion operation should be fast, but as already written, this is clearly not the case when it needs to be used.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question