Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much processing and memory use does casting take in Java?

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.

How much system resources does this use:

objectName.functionOne();
((SubClass) objectName).functionOther();

Is it better than:

SuperClass objectA = (SuperClass) getSameInstance();
SubClass objectB = getSameInstance();
objectA.functionOne();
objectB.functionOther();

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:

((SubClass) objectName).functionOther();

However, is it worth it?

Thanks,
Grae

UPDATE:

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.

I could just have:

SuperClass instanceRef;
SubClass instanceRef2;

instanceRef.etc()
instanceRef.etc2()
instanceRef.etc3()
instanceRef2.specialSubClassOnlyCall();
instanceRef2.specialSubClassOnlyCall2();

or I could have:

SuperClass instanceRef;

instanceRef.etc()
instanceRef.etc2()
instanceRef.etc3()
((SpecialSubClass)instanceRef).specialSubClassOnlyCall();
((SpecialSubClass)instanceRef).specialSubClassOnlyCall2();

However, I don't know which is more efficient.

UPDATE 2:

Here is an example to show you what I am talking about:

class Shape
Triangle extends Shape
Square extends Shape
Circle extends Shape
Cube extends Shape

The Two Pointer Example: (Downside an extra pointer.)

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();
}

Or I could do it this way. (Downside a bunch of casts.)

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();
}

So is five casts worse than one extra pointer? What is it was six casts, or 20? Is one cast worse than the pointer.

Grae

like image 399
GC_ Avatar asked Oct 19 '10 13:10

GC_


People also ask

Is Java casting expensive?

To answer your questions. Up casting usually costs virtually nothing, (when you change the reference type to a parent class of the object). Knowledge of the reference type is enough to decide if uptyping is valid, it just gets the class loader to look up the inheritance map.

Does casting affect performance?

It won't affect program performance, but it will affect development and maintenance performance. Now that Java has generics, there is rarely a need to cast an Object to a more specific type.

Does casting take time?

It depends. A lot of casts are effectively instructions to the compiler to do something a certain way, and don't have any execution time consequences at all. On the other hand, conversion from floating point to integer types on the arduino can take quite a lot of instructions and time.

What happens when casting Java?

Type casting is a way of converting data from one data type to another data type. This process of data conversion is also known as type conversion or type coercion. In Java, we can cast both reference and primitive data types. By using casting, data can not be changed but only the data type is changed.


1 Answers

Do whatever makes the code clearer, forget about micro-optimizations.

like image 137
Michael Borgwardt Avatar answered Sep 20 '22 14:09

Michael Borgwardt