Comparing Performance of int and Integer

后端 未结 5 2084
天涯浪人
天涯浪人 2020-12-18 18:26

Which one is best in programming - int or Integer? Especially whenever both are doing the same task?

I am writing an appli

5条回答
  •  天命终不由人
    2020-12-18 19:18

    Use int when possible, and use Integer when needed. Since int is a primitive, it will be faster. Modern JVMs know how to optimize Integers using auto-boxing, but if you're writing performance critical code, int is the way to go.

    Take a look at this and this article. Although you shouldn't treat them as absolute truths, they do show that objects will be slower than their primitive counterparts.

    So, use int whenever possible (I will repeat myself: if you're writing performance critical code). If a method requires an Integer, use that instead.

    If you don't care about performance and want to do everything in an object oriented fashion, use Integer.

提交回复
热议问题