?
Solved

Efficiency of typecasting

Posted on 2004-09-03
4
Medium Priority
?
436 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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:
Suggested Courses
Course of the Month11 days, 19 hours left to enroll

752 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