C
C
click_f2017-06-26 16:56:10
Java
click_f, 2017-06-26 16:56:10

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

2 answer(s)
F
Fat Lorrie, 2017-06-26
@click_f

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.

A
Andrey K, 2017-06-27
@kuftachev

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 question

Ask a Question

731 491 924 answers to any question