开发者

primitive vs managed types on android - which one should i use in my app

im trying to port an iphone app to android. i am completely new to java and android environment

i see different code samples on the internet, some use int and boolean where as some use Integer and Boolean

i开发者_开发百科 wanted to know the pros and cons of both and which is the best way forward?

since primitive types are saved on the stack and no garbage collection happens on them, are they definitely a better choice as far as performance goes?

what is the real benefit of primitive v/s managed types? and what is the suggested approach?

in my app performance is crucial...

thanks in advance


The wrapper types are used when you need an object. For example : to be able to store integers in a collection (like a List or a Set), you need to transform the primitive into an object (Integer).

The wrapper have another feature over the primitive types : they are nullable.So, for example, to represent an integer value stored in a nullable column of a database table, you'll use Integer rather than int to be able to store the null value.

Basicaly, my advice would be : use primitive types except whan you can't (for example, for one of the reasons I just explained).


Generally, stick to int, boolean, etc. but watch out for excessive autoboxing. For example, in a tight loop make sure that you are not wrapping/unwrapping them unnecessarily.

Eclipse can warn you about them. Probably other IDEs can do this also.


Any allocations on the UI thread can cause performance issues because the garbage collection kicks in at this point and it can (on a bad day) take 1/4 of a second which is visible on screen refreshes. Therefore as @JB Nizet says use primitives when you can. The Trove primitive collections libraries can help with this, though i'd suggest running the libraries or your app through pro-guard to limit the size of the library.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜