title: java: combined instanceof and cast?
body: <p>(Please no advise that I should abstract <code>X</code> more and add another method to it.)</p>
  
  <p>In C++, when I have a variable <code>x</code> of type <code>X*</code> and I want to do something specific if it is also of type <code>Y*</code> (<code>Y</code> being a subclass of <code>X</code>), I am writing this:</p>
  
  <pre><code>if(Y* y = dynamic_cast&lt;Y*&gt;(x)) {
      // now do sth with y
  }
  </code></pre>
  
  <p>The same thing seems not possible in Java (or is it?).</p>
  
  <p>I have read this Java code instead:</p>
  
  <pre><code>if(x instanceof Y) {
      Y y = (Y) x;
      // ...
  }
  </code></pre>
  
  <p>Sometimes, when you don't have a variable <code>x</code> but it is a more complex expression instead, just because of this issue, you need a dummy variable in Java:</p>
  
  <pre><code>X x = something();
  if(x instanceof Y) {
      Y y = (Y) x;
      // ...
  }
  // x not needed here anymore
  </code></pre>
  
  <p>(Common thing is that <code>something()</code> is <code>iterator.next()</code>. And there you see that you also cannot really call that just twice. You really need the dummy variable.)</p>
  
  <p>You don't really need <code>x</code> at all here -- you just have it because you cannot do the <code>instanceof</code> check at once with the cast. Compare that again to the quite common C++ code:</p>
  
  <pre><code>if(Y* y = dynamic_cast&lt;Y*&gt;( something() )) {
      // ...
  }
  </code></pre>
  
  <p>Because of this, I have introduced a <code>castOrNull</code> function which makes it possible to avoid the dummy variable <code>x</code>. I can write this now:</p>
  
  <pre><code>Y y = castOrNull( something(), Y.class );
  if(y != null) {
      // ...
  }
  </code></pre>
  
  <p>Implementation of <code>castOrNull</code>:</p>
  
  <pre><code>public static &lt;T&gt; T castOrNull(Object obj, Class&lt;T&gt; clazz) {
      try {
          return clazz.cast(obj);
      } catch (ClassCastException exc) {
          return null;
      }
  }
  </code></pre>
  
  <p>Now, I was told that <a href="https://stackoverflow.com/questions/3933609/java-unchecked-cast-from-x-to-y-how-to-implement-castornull/3933848#3933848">using this <code>castOrNull</code> function in that way is an evil thing do to</a>. Why is that? (Or to put the question more general: Would you agree and also think this is evil? If yes, why so? Or do you think this is a valid (maybe rare) use case?)</p>
  
  <hr>
  
  <p>As said, I don't want a discussion whether the usage of such downcast is a good idea at all. But let me clarify shortly why I sometimes use it:</p>
  
  <ol>
  <li><p>Sometimes I get into the case where I have to choose between adding another new method for a very specific thing (which will only apply for one single subclass in one specific case) or using such <code>instanceof</code> check. Basically, I have the choice between adding a function <code>doSomethingVeryVerySpecificIfIAmY()</code> or doing the <code>instanceof</code> check. And in such cases, I feel that the latter is more clean.</p></li>
  <li><p>Sometimes I have a collection of some interface / base class and for all entries of type <code>Y</code>, I want to do something and then remove them from the collection. (E.g. I had the case where I had a tree structure and I wanted to delete all childs which are empty leafs.)</p></li>
  </ol>