16

I'm new to programming. I'm sure the answer for this question is out there, but I have no idea what to search for.

Ok, I'll go right to it.

Here's my code:

int[] arr;
arr = new int[5];

arr[0] = 20;
arr[1] = 50;
arr[2] = 40;
arr[3] = 60;
arr[4] = 100;

System.out.println(arr);

This compiles and works fine. It's just the output from CMD that I'm dizzy about.

This is the output: [I@3e25a5.

I want the output to represent the exact same numbers from the list (arr) instead. How do I make that happen?

0xCursor
  • 2,242
  • 4
  • 15
  • 33
Racket
  • 327
  • 3
  • 6
  • 15
  • @Duncan Jones Funny that you marked this post as a duplicate when it came like 4 years before the other post... I understand that the linked post is a tutorial like post, though. – 0xCursor Jul 29 '18 at 15:07
  • 1
    @LAD Yes, it's a bit odd. But it's a common practice here - you can close older questions as duplicates if there is a more detailed answer. – Duncan Jones Jul 29 '18 at 17:26

10 Answers10

28

Every object has a toString() method, and the default method is to display the object's class name representation, then @ followed by its hashcode. So what you're seeing is the default toString() representation of an int array. To print the data in the array, you can use:

System.out.println(java.util.Arrays.toString(arr));

Or, you can loop through the array with a for loop as others have posted in this thread.

0xCursor
  • 2,242
  • 4
  • 15
  • 33
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
8
System.out.println(Arrays.toString(arr));

The current output is classtype@hashcode.

Incase you need to print arrays with more than one dimension use:

Arrays.deepToString(arr);

Also remember to override toString() method for user-defined classes so that you get a representation of the objet as you choose and not the default represention which is classtype@hashcode

Emil
  • 13,577
  • 18
  • 69
  • 108
5

It's the default string representation of array (the weird text).

You'll just have to loop through it:

for(int i : arr){
System.out.println(i);
}
Goran Jovic
  • 9,418
  • 3
  • 43
  • 75
5

To print the values use.

for(int i=0; i<arr.length; i++)
   System.out.println(arr[i]);
Enrique
  • 9,920
  • 7
  • 47
  • 59
  • 1
    not a memory address. Class Name + "@" + hashcode. – Vincent Ramdhanie Dec 18 '10 at 19:27
  • You are right. I'll edit that – Enrique Dec 18 '10 at 19:31
  • Well, it kind of is a memory address in many if not most situations. The Object API states that the result of toString is: getClass().getName() + '@' + Integer.toHexString(hashCode()) and the API further states that in order for hashCode to try to return as unique a number as possible: This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language. So if hashCode and toString haven't been overridden, you'll see an internal address. – Hovercraft Full Of Eels Dec 18 '10 at 19:56
4

Like this:

for (int i = 0; i < arr.length; ++i)
{
    System.out.println(arr[i]);
}

That "weird number" is the reference for the array you printed out. It's the default behavior built into the java.lang.Object toString() method.

You should override it in your own objects if seeing the reference isn't sufficient.

duffymo
  • 305,152
  • 44
  • 369
  • 561
4

It prints it's .toString() method you should print each element

for(int i=0; i<arr.length; i++) {
   System.out.println(arr[i]);
}

or

for(Integer i : arr) {
  System.out.println(i);
}
user547215
  • 116
  • 2
4

BTW You can write

int[] arr = { 20, 40, 60, 40, 60, 100 };
System.out.println(Arrays.toString(array));

or even

System.out.println(Arrays.toString(new int[] { 20, 40, 60, 40, 60, 100 }));

or

System.out.println(Arrays.asList(20, 40, 60, 40, 60, 100));
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
3
for (int i = 0; i < arr.length; ++i)
{
  System.out.println(arr[i]);
}
Oswald
  • 31,254
  • 3
  • 43
  • 68
2

Use Arrays.toString() and PrintStream.printf(String format, Object... args).

System.out.printf("%s%n", Arrays.toString(arr));
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Anton Dozortsev
  • 4,782
  • 5
  • 34
  • 69
-3

You printed the reference and not the values at the reference... One day it will all become clear with C.

EnabrenTane
  • 7,428
  • 2
  • 26
  • 44