You cannot do
int i = 2;
String two = i.toString(); // or sample[1].toString();
As int
is a primitive type, not an object. As you're working with the int[] sample
array, notice that while sample
is an object, sample[1]
is just an int
, so the primitive type again.
Instead, you should do
int i = 2;
String two = String.valueOf(i); // or String.valueOf(sample[1]);
But if your problem is just printing the value to System.out
, it has a println(int x)
method, so you can simply do
int i = 2;
System.out.println(i); // or System.out.println(sample[1]);
Now, if you want to print the complete representation of your array, do
System.out.println(Arrays.toString(sample));