Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing an array without stating which one leads leads to a String of random code

Tags:

java

arrays

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?

like image 803
dot Avatar asked Feb 13 '16 07:02

dot


People also ask

How do I print an array in random order?

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.

How do I print an array into a string?

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.

How to print an array in Java without any loop?

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.

How to get the string representation of an array in Java?

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.

What is an array in Java?

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:

What is the use of println () method in it?

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.


2 Answers

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
like image 137
Jim Garrison Avatar answered Sep 24 '22 19:09

Jim Garrison


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)

like image 31
Nikolay Tomitov Avatar answered Sep 22 '22 19:09

Nikolay Tomitov