Does returning an int variable as an Integer instead of int affect performance?


I have a custom list class and an interface IList. I want to make specialized versions of my custom list class for all the primitive types (int, short, boolean, etc) but I still want to them to implement IList. Because of that the specialized version of my custom int list, for example, would implement IList and the get() method would return Integer not int. The reason I want to make specialized lists for primitives is in order to use an int[] array inside my custom list class instead of a T[] array since casting does have a certain cost and I would like it to be very efficient.

Therefore I would like to know if it will affect perfomance if I have to implement IList for my specialized class. If I were not to implement IList the get() method for example would simply return int and there wouldn't be any concerns. Still I would like to preserve my abstractions and because of that I want to know want to know if implementing IList will affect performance or not.

class Test
    int m_Integer = 0;

//Both methods return the variable m_Integer which is of type int, a primitive, but one method returns it using int and the other using Integer public int getUsingPrimitive() { return m_Integer; } public Integer getUsingClass() { return m_Integer; } public void test() { int b; b = getUsingPrimitive(); //Is this method b = getUsingClass(); //more efficient than this method //Im interested in whether there is a performance difference between the two methods }


EDIT: My question is not asking whether I should use int vs Integer. I am quite aware of the differences between int and Integer in Java. My question is whether in this specific case where I have an int variable returning it via a method that returns Integer affects the perfomance compared to returning it via a method that returns int.

Show source
| java   | performance   | arrays   2017-01-05 18:01 0 Answers

Answers ( 0 )

◀ Go back