Solved

Efficiency of typecasting

Posted on 2004-09-03
4
432 Views
Last Modified: 2011-10-03
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
Comment
Question by:sudhakar_koundinya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 9

Assisted Solution

by:DrWarezz
DrWarezz earned 20 total points
ID: 11971117
:o\ Don't think so..
[r.D]
0
 
LVL 6

Expert Comment

by:expertmb
ID: 11971134
>>will it reduce the efficiency of method??
it should not, you are now making different kind of reference to the object.
0
 
LVL 6

Assisted Solution

by:expertmb
expertmb earned 20 total points
ID: 11971141
objects are still there, you are accessing messages(methods) with different reference.
0
 
LVL 35

Accepted Solution

by:
TimYates earned 20 total points
ID: 11971214
> 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question