3

What is the performance difference between Integer.valueOf() and Autoboxing?

This is my below code:

    int value = 5;

    //1 Integer.valueOf()
    Integer result =  Integer.valueOf(5);

    //2 Autoboxing
    Integer result = value;  

Note: I need Integer Object. Ex: use it as a key in a HashMap< Integer,String>

I don't know why and which is faster? Integer.valueOf() (1) or Autoboxing (2).

About (1) I check java code of Integer.valueOf(). There seem they get Integer object from cache.

    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

About (2) I heard that JVM has its own Integer pool to reuse Integer object.

I try to understand but still don't know why and which is faster?

Hieu Tran
  • 33
  • 1
  • 6
  • 2
    Neither. They are the same speed (or near enough to never matter in practice). If you want "faster" use `int` (the entire justification for primitive types existing in Java is they're "faster"). Autoboxing is easier to read, so use that if you must have a wrapper type. – Elliott Frisch Jan 10 '20 at 04:45
  • @ElliottFrisch thanks for your suggest, but i need the Integer object to be return in my function. – Hieu Tran Jan 10 '20 at 04:48
  • @VishwaRatna actually I have an int value, so I just confuse which way is better to get Integer object. I understand parseInt is for also String. – Hieu Tran Jan 10 '20 at 04:51
  • @HieuTran Posting that function would have been helpful; please help us help you by providing relevant information in your question (there is great honor in using the edit button to *improve* the utility of your question for future user's seeking the same help). Your question was which is faster; and I have answered neither. Prefer readable code to faster code. In **fact**, [write dumb code](https://www.oracle.com/technical-resources/articles/javase/devinsight-1.html). And *premature optimization is the root of all evil.* – Elliott Frisch Jan 10 '20 at 04:52
  • Finally, if "a" were faster than "b" then you can assume the clever compiler writer would transform "b" into "a". Further, the inquisitive knowledge seeker might examine their bytecode with `javap -v` (how do you think autoboxing actually works?) – Elliott Frisch Jan 10 '20 at 04:55
  • You would not be able to measure the difference, if indeed there is one. Worrying about this level of micro-optimization is a fruitless exercise. – Bohemian Jan 10 '20 at 05:17
  • 1
    The byte code is identical. Autoboxing uses `Integer.valueOf()` under the hood. The performance is therefore also identical. – user207421 Jan 10 '20 at 06:09
  • @ElliottFrisch Thank you very much for your value advices. I have just edit the question for more details. I have this question because I see that Interger.ValueOf get Interger object from IntegerCache but I heard that autoboxing seem get Interger object from Integer pool. – Hieu Tran Jan 10 '20 at 06:12
  • The `IntegerCache` is the Integer pool. – Elliott Frisch Jan 10 '20 at 06:12
  • 1
    @HieuTran Could you please make the effort to spell `Integer` and `Integer.valueOf()` correctly. – user207421 Jan 10 '20 at 06:17
  • I understood the answer is the same performance for (1) and (2). Thank everyone for helping me. – Hieu Tran Jan 10 '20 at 06:20
  • @user207421 Okay, sorry for my bad English. – Hieu Tran Jan 10 '20 at 06:32
  • You spelt it properly for the compiler. No reason to get it wrong elsewhere. – user207421 Jan 10 '20 at 06:37
  • @user207421 Actually my reason is I did copy and paste for the compiler but do typing when comment here :) – Hieu Tran Jan 10 '20 at 06:42

1 Answers1

3

This question is strongly related to this question. As already said in the comments and in the answer to the linked question,

autoboxing invokes the static method Integer.valueOf(), and autounboxing invokes intValue() on the given Integer object. There's nothing else, really - it's just syntactic sugar.

Obviously, the performance is the same. However, things are a bit more complicated as this answer says:

there is no guarantee of how autoboxing is internally implemented.

So, in theory, given some exotic java compiler, the implementation might differ and so might the performance. Practically, there's no reason for implementing autoboxing differently. Moreover, if there was a better implementation, it probably could be incorporated into Integer.valueOf(). So even then, the performance would be the same.


In Java, there's usually nothing to gain from using an alternate implementation doing the same. For example, there used to be performance differences between Arrays.copyOf and System.arraycopy, but AFAIK they were optimized away in the Oracle / OpenJDK JVM.

maaartinus
  • 44,714
  • 32
  • 161
  • 320
  • thank you very much for your detail answer :) Sorry, this is my first question, I have just join stackoverflow and could not vote up your answer now. – Hieu Tran Jan 12 '20 at 15:46