While I was working on a project of mine, I tried to print out an integer from an array using the following code:
Random dice = new Random();
int wolfhealth[] = new int[]{dice.nextInt(15)+9};
System.out.println(wolfhealth);
I accidentally forgot to state which integer I wanted to print out from the array of integers which lead to it printing out this line of code:
[I@75b84c92
I have already fixed the issue by changing the 3rd line into
System.out.println(wolfhealth[0]);
My question is, what does this line of code - [I@75b84c92
mean exactly? Is it some kind of unique identifier or ID for the array?
create an instance of the random class and then use the integer i which will be the index. where it lands, compute before element and after element length and then do the same recursively.
We cannot print array elements directly in Java, you need to use Arrays. toString() or Arrays. deepToString() to print array elements. Use toString() method if you want to print a one-dimensional array and use deepToString() method if you want to print a two-dimensional or 3-dimensional array etc.
This article tells how to print this array in Java without the use of any loop. For this, we will use toString () method of Arrays class in the util package of Java. This method helps us to get the String representation of the array. This string can be easily printed with the help of print () or println () method.
Concept: We will be using the toString () method of the Arrays class in the util package of Java. This method helps us to get the String representation of the array. This string can be easily printed with the help of the print () or println () method.
Java array is a data structure where we can store the elements of the same data type. The elements of an array are stored in a contiguous memory location. So, we can store a fixed set of elements in an array. There are following ways to print an array in Java:
It has a method called println (). It is an overloaded method which can accept anything as an argument. When we put println () method after member access operator (::), it becomes an expression.
That is output from the default toString()
implementation from Object
, as "overridden" for array objects.
The output encodes the type and memory address:
[ I @75b84c92
▲ ▲ ▲
│ │ └─── hash code
│ └─── of integer
└─── array
Arrays in java are referent types. That means that you actually store a reference(an address) in stack memory to some other memory in heap. The first 2 symbols mean that this is an array of ints ([I) and latter mean that it located in some address in heap space (@ 75b84c92). Hope this helps to understand it. You may also see a contents of an array (mainly for debug purposes) using Arrays.toString(a)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With