-2

I am getting confused why I can't use the statement to display the values in an array?

System.out.println(intarray);

This is the output '[I@7852e922'

If java is pass-by-value shouldnt that statement return the values of the array not the object reference. This is the fix im using.

System.out.println(Arrays.toString(intarray));
  • I don't think `Array` have a `toString()` method. So it tries to display something it doesn't know how to display ... – Lutzi May 08 '18 at 07:32
  • Your question does not make sense. Pass by value and printing an object are not related at all. If you had googled "java pass by value", you would have found this answer: https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value which specifically says: "Java is always pass-by-value. Unfortunately, they decided to call the location of an object a "reference". When we pass the value of an object, we are passing the reference to it. " – nbokmans May 08 '18 at 07:32
  • 1
    @Lutzi An array (even for primitive types) is an Object as any other objects as well and therefore at least inherits `toString()` from `java.lang.Object`. – L.Spillner May 08 '18 at 07:34
  • It herits it yes, but don't know how to display value. It's not because you specialize Object that Java knows how to print it (e.g. a specialization of Object with two fields). – Lutzi May 08 '18 at 07:36
  • 1
    @Lutzi the direct implementation of how the String is evaluated: [Object.toString()](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/lang/Object.java#Object.toString%28%29) – L.Spillner May 08 '18 at 07:38

4 Answers4

5

This has nothing to do with Java being pass by value.

System.out.println(intarray) uses the toString() method to obtain a String representation of the passed object.

The default implementation of Object's toString prints what you call "gibberish" (which is generated by the expression getClass().getName() + "@" + Integer.toHexString(hashCode())). The array class does not override that implementation. Therefore, System.out.println(intarray) prints "gibberish".

On the other hand, Arrays.toString(intarray) returns a String that lists the elements of the passed array.

Eran
  • 387,369
  • 54
  • 702
  • 768
1

Java passed the value of the reference intarray, which means it passes the address of the array.

What it prints is not related to passing parameters by value or not, it depends on what the print method does.

Vincent Cantin
  • 16,192
  • 2
  • 35
  • 57
1

When you do

System.out.println(myObj);

the object myObj is converted to a printable format (i.e., a String) by calling myObj.toString().

If an object doesn't provide its own toString() implementation, it inherits the toString() from Object, which returns object's class name concatenated with the object's hashcode, as follows (javadoc):

getClass().getName() + '@' + Integer.toHexString(hashCode())

Arrays inherit toString() from Object and this is why you get the output [I@7852e922. Note that the class name is in fact [I, which represents "an array containing the primitive type int" -- you can read more about class names here.

k_ssb
  • 6,024
  • 23
  • 47
0

It does not matter whether Java is pass by value here. The reason it is printing the hashcode is because Array is an Object in Java.

When you try to print an Object in Java using System.out.println(), the String representation of the Object will be displayed. That is, the toString() method of Array (inherited from Object class) will be implicitly invoked, thus giving you the so called "gibberish".

user3437460
  • 17,253
  • 15
  • 58
  • 106