• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 448
  • Last Modified:

Efficiency of typecasting

Hi All,

I just get the doubt. Could you please clarify that

java.util.List contains either strings or my own defined class instances

now when I try to type cast them to Array, will it reduce the efficiency of method??


I  am type casting like this. List's size may vary from 1 to 100000

(String[])list.toArray(new String[0]);  

        or

(IWdHttpRefernce[])list.toArray(new IWdHttpRefernce[0]);  

0
sudhakar_koundinya
Asked:
sudhakar_koundinya
  • 2
3 Solutions
 
DrWarezzCommented:
:o\ Don't think so..
[r.D]
0
 
expertmbCommented:
>>will it reduce the efficiency of method??
it should not, you are now making different kind of reference to the object.
0
 
expertmbCommented:
objects are still there, you are accessing messages(methods) with different reference.
0
 
TimYatesCommented:
> now when I try to type cast them to Array, will it reduce the efficiency of method??

toArray does this:

    public Object[] toArray(Object a[])
    {
        if (a.length < size)
            a = (Object[])java.lang.reflect.Array.newInstance( a.getClass().getComponentType(), size );
      System.arraycopy(elementData, 0, a, 0, size);
        if (a.length > size)
            a[size] = null;
        return a;
    }

But then, you will be accessing items in an array directly, rather than calling a method (which accesses elements in an array directly) ;-)

The difference will be minimal (at most) ;-)
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now