Search code examples
javaunboxing

Should I Box or unbox in the assert?


Assuming that I have a method foo:

public Integer foo () { return 1; }

Which of the above to options is lesser expensive? Boxing or unboxing?

assert(1, (int) foo()));

vs.

assert((Integer)3, foo());

Solution

  • I think primitive type variable comparison is more faster than object comparison. also boxing will take more time to create wrapper object. so you should use unboxing in your assertion.