You can use
Integer integer = Integer.valueOf(i);
From the javadoc of the constructor:
Deprecated. It is rarely appropriate to use this constructor. The static factory valueOf(int) is generally a better choice, as it is likely to yield significantly better space and time performance. Constructs a newly allocated Integer object that represents the specified int value.
The main difference is that you won't always get a new instance with valueOf as small Integer instances are cached.
All of the primitive wrapper types (Boolean, Byte, Char, Short, Integer, Long, Float and Double) have adopted the same pattern. In general, replace:
new <WrapperType>(<primitiveType>)
with
<WrapperType>.valueOf(<primitiveType>)
(Note that the caching behavior mentioned above differs with the type and the Java platform, but the Java 9+ deprecation applies notwithstanding these differences.)
Answer from Denys Séguret on Stack OverflowYou are trying to initialize it on itself - you are passing i as an argument, not something like 5.
public Integer(int value)
Constructs a newly allocated Integer object that represents the specified int value.
this is the documentation of integer, you are passing and integer i that the current value is null, to a constructor that return and integer from and int value, the correct form will be like this
Integer i = new Integer(5);
and you are doing something like this
int i = i;
I hope that help you.
From the documentation:
It is rarely appropriate to use this constructor. The static factory
valueOf(int)is generally a better choice, as it is likely to yield significantly better space and time performance.
So do this:
public String toString() {
return m.get(row) + Integer.valueOf(diagonal).toString();
}
You can simply convert the Integer to a string using the built-in method toString():
return m.get(row) + Integer.toString(diagonal);