开发者

Casting and Generics, Any performance difference?

I am coding in Android a lot lately, Though I am comfortable in JAVA, but missing some ideas about core concepts being used there.

I am interested to know whether any performance difference is there between these 2 codes.

First Method:

//Specified as member variable.   
ArrayList <String> myList  = new ArrayList <String>(); 

and using as String temp = myList.get(1);

2nd Method:

ArrayList myList  = new ArrayList(); //Specified as member variable.   

and using

String temp1 = myList.get(1).toString();   

I know its about casting. Does the first method has great advantage over the second, Most of the time in real coding I have to use second method because arraylist can take different data types, I end up specifying

A开发者_开发知识库rrayList <Object> = new ArrayList <Object>(); 

or more generic way.


In short, there's no performance difference worth worrying about, if it exists at all. Generic information isn't stored at runtime anyway, so there's not really anything else happening to slow things down - and as pointed out by other answers it may even be faster (though even if it hypothetically were slightly slower, I'd still advocate using generics.) It's probably good to get into the habit of not thinking about performance so much on this level. Readability and code quality are generally much more important than micro-optimisations!

In short, generics would be the preferred option since they guarantee type safety and make your code cleaner to read.

In terms of the fact you're storing completely different object types (i.e. not related from some inheritance hierarchy you're using) in an arraylist, that's almost definitely a flaw with your design! I can count the times I've done this on one hand, and it was always a temporary bodge.


Generics aren't reified, which means they go away at runtime. Using generics is preferred for several reasons:

  • It makes your code clearer, as to which classes are interacting
  • It keeps it type safe: you can't accidentally add a List to a List
  • It's faster: casting requires the JVM to test type castability at runtime, in case it needs to throw a ClassCastException. With Generics, the compiler knows what types things must be, and so it doesn't need to check them.


There is a performance difference in that code: The second method is actually slower.

The reason why:

Generics don't require casting/conversion (your code uses a conversion method, not a cast), the type is already correct. So when you call the toString() method, it is an extra call with extra operations that are unnecessary when using the method with generics.

There wouldn't be a problem with casting, as you are using the toString() method. But you could accidentally add an incorrect object (such as an array of Strings). The toString() method would work properly and not throw an exception, but you would get odd results.


As android is used for Mobiles and handheld devices where resources are limited you have to be careful using while coding. Casting can be overhead if you are using String data type to store in ArrayList. So in my opinion you should use first method of being specific.


There is no runtime performance difference because of "type erasure".

But if you are using Java 1.5 or above, you SHOULD use generics and not the weakly typed counterparts.

Advantages of generics --

* The flexibility of dynamic binding, with the advantage of static type-checking. Compiler-detected errors are less expensive to repair than those detected at runtime.
* There is less ambiguity between containers, so code reviews are simpler.
* Using fewer casts makes code cleaner. 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜