Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Efficiency of typecasting

Posted on 2004-09-03
4
Medium Priority
?
440 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 60 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 60 total points
ID: 11971141
objects are still there, you are accessing messages(methods) with different reference.
0
 
LVL 35

Accepted Solution

by:
TimYates earned 60 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

618 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