Solved

Efficiency of typecasting

Posted on 2004-09-03
4
423 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

733 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