Search code examples
javaoopcasting

How can I convert an Object type to a int type in Java?


I'm looking for a way to convert a Object type (as in the wildcard type which can store many different types) to an int type. In all of my searching I have not found an answer to this question on the site.

I have a Object[] array which stores only integers and I need to perform some math using one of the integers. For example int result = array[3] + 2;. This arithmetic won't work unless I can cast/convert that Object to an int.


Solution

  • The others that say Integer[] and int[] are the solutions to the question is very true when only numbers will be added in the array. But, In some cases, there are requirements that we should follow. So Object that contains number can be casted to Integer and unbox it to int is more likely what the OP needs.