title: How much processing and memory use does casting take in Java?
body: <p>I am considering whether it is better to have two pointers, one for each object sub class and super, or whether I should just use casting.</p>
  <p>How much system resources does this use:</p>
  <pre><code>objectName.functionOne();
  ((SubClass) objectName).functionOther();
  </code></pre>
  <p>Is it better than:</p>
  <pre><code>SuperClass objectA = (SuperClass) getSameInstance();
  SubClass objectB = getSameInstance();
  objectA.functionOne();
  objectB.functionOther();
  </code></pre>
  <p>Basically, my main question is about the resources used with casting, versus making an extra pointer.  It seems like I could save several in line casts, such as:</p>
  <pre><code>((SubClass) objectName).functionOther();
  </code></pre>
  <p>However, is it worth it?</p>
  <p>Thanks,<br/>
  Grae</p>
  <h3>UPDATE:</h3>
  <p>There were some unclear parts to my question.  Basically, I have a super class that I am using through out a large function.  It works with three subclasses.  Some the super class is working as I would like.  However, I hit a road block in a few places where I have to use a function from one of the three different subclass; a function that is only in one of the subclasses.</p>
  <p>I could just have:</p>
  <pre><code>SuperClass instanceRef;
  SubClass instanceRef2;
  
  instanceRef.etc()
  instanceRef.etc2()
  instanceRef.etc3()
  instanceRef2.specialSubClassOnlyCall();
  instanceRef2.specialSubClassOnlyCall2();
  </code></pre>
  <p>or I could have:</p>
  <pre><code>SuperClass instanceRef;
  
  instanceRef.etc()
  instanceRef.etc2()
  instanceRef.etc3()
  ((SpecialSubClass)instanceRef).specialSubClassOnlyCall();
  ((SpecialSubClass)instanceRef).specialSubClassOnlyCall2();
  </code></pre>
  <p>However, I don't know which is more efficient.</p>
  <h3>UPDATE 2:</h3>
  <p>Here is an example to show you what I am talking about:</p>
  <pre><code>class Shape
  Triangle extends Shape
  Square extends Shape
  Circle extends Shape
  Cube extends Shape
  </code></pre>
  <p>The Two Pointer Example:   (Downside an extra pointer.)</p>
  <pre><code>Shape pointer1 = (Shape) getSomeRandomShape();
  Cube pointer2 = null;
  
  pointer1.getWidth();
  pointer1.getHeight();
  pointer1.generalShapeProp();
  pointer1.generalShapeProp2();
  pointer1.generalShapeProp3();
  
  if(sure_its_cube)
  {
     pointer2 = (Cube) pointer1;
     pointer2.getZAxis();
     pointer2.getOtherCubeOnlyThing();
     pointer2.getOtherCubeOnlyThing2();
     pointer2.getOtherCubeOnlyThing3();
     pointer2.getOtherCubeOnlyThing4();
  }
  </code></pre>
  <p>Or I could do it this way.  (Downside a bunch of casts.)</p>
  <pre><code>Shape pointer1 = (Shape) getSomeRandomShape();
  
  pointer1.getWidth();
  pointer1.getHeight();
  pointer1.generalShapeProp();
  pointer1.generalShapeProp2();
  pointer1.generalShapeProp3();
  
  if(sure_its_cube)
  {
     ((Cube)pointer1).getZAxis();
     ((Cube)pointer1).getOtherCubeOnlyThing();
     ((Cube)pointer1).getOtherCubeOnlyThing2();
     ((Cube)pointer1).getOtherCubeOnlyThing3();
     ((Cube)pointer1).getOtherCubeOnlyThing4();
  }
  </code></pre>
  <p>So is five casts worse than one extra pointer?   What is it was six casts, or 20?  Is one cast worse than the pointer.</p>
  <p>Grae</p>