• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Printing a primitive array

 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int arr[] = new int[]{ 1,2,3,41 };
System.out.println(months);

This prints some junk!! - [I@19821f or [I@3e25a5 - random of one of these every time this code is run
Why is this not working?
Is Arrays.toString(arr) the only way to print a primitive array?
 
Henry Wong
author
Marshal
Pie
Posts: 21419
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vinoth kumar k wrote:int arr[] = new int[]{ 1,2,3,41 };
System.out.println(months);


This prints some junk!! - [I@19821f or [I@3e25a5 - random of one of these every time this code is run
Why is this not working?
Is Arrays.toString(arr) the only way to print a primitive array?



The println() calls the toString() method to convert the object to a string before printing it. And arrays do not override the toString() method, so it is inherited from the object class.

The toString() method of the Object class prints the object type, followed by an @, followed by the identity hashcode in hex... so... [I@3e25a5 means array "[" of int "I" with hashcode 3e25a5.

Henry
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
iterating through an array is another way to output.

 
subhash kumar
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To print an array nicely formatted, you can also use Arrays.toString:

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
The println() calls the toString() method to convert the object to a string before printing it. And arrays do not override the toString() method, so it is inherited from the object class.
Henry


Henry, one question, which class you are mentioned here?
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:
Henry, one question, which class you are mentioned here?

The array class
http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:
Abimaran Kugathasan wrote:
Henry, one question, which class you are mentioned here?

The array class
http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html


You mean to say Arrays class, there is no class in the API with the name array, that's why I asked!
 
Campbell Ritchie
Sheriff
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The array class in the current question is the class called [I. Whether this is a notional class or an actual class is not important, but it is "whichever class arrays are made from."
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The array class in the current question is the class called [I. Whether this is a notional class or an actual class is not important, but it is "whichever class arrays are made from."


Campbell, I couldn't get it.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anytime you use "System.out.println()" and pass in an object reference, java will call that underlying object's toString() method. It doesn't matter if it's an array, a String, an Integer, a Dog, a Fubar or what.

In your example, you are passing in an object that is an array (doesn't matter what the specific type is). Since the Arrays class does not override the Object class' toString() method, you get the default behavior of the Object class' toString() method.

You would see something VERY similar if you created your own Fubar class and did a System.out.println on it.

However, if in your class definition, you override the toString() method with something meaningful, when you sent it to S.o.p(), you will get your newly defined toString() behavior.
 
Henry Wong
author
Marshal
Pie
Posts: 21419
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:
Campbell Ritchie wrote:The array class in the current question is the class called [I. Whether this is a notional class or an actual class is not important, but it is "whichever class arrays are made from."


Campbell, I couldn't get it.



Basically, the java compiler create "array" classes, that mirror the regular classes, and for the primative types.

Meaning there is a class type called Object[], String[]. StringBuffer[]. Integer[], etc. And for the primatives, class types called int[], float[], char[], etc. These classes are created on-the-fly by the compiler (or the JVM, not sure). There is no java source for these classes, and hence, there is no JavaDoc for these classes.

Henry
 
Campbell Ritchie
Sheriff
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Henry has explained it far better than I would have.
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As for the [I in the output, this is how these classes are named: [ to indicate it's an array, followed by the native (JNI) name:
- Z for boolean (because B is used elsewhere)
- B for byte
- C for char
- S for short
- I for int
- J for long (because L is used elsewhere)
- F for float
- D for double
- L followed by the fully qualified name and a ; for objects; e.g. [Ljava.lang.String; for String[]

Each additional dimension simply gets a [ at the start; e.g. [[I for int[][].

You don't need to remember this (unless you're doing JNI programming), but it may be a fun fact for you.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry Wong and Campbell Ritchie, for this information, could you Please confirm the following....

Is this correct? I mean the hierarchy of the arrays?


Thanks in Advanced!
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes and no.
Yes, Object[][] is a subtype of Object[] -- you can assign an Object[][] to an Object[], and instanceof also works this way.
No, Object[] is not a subclass of Object[], its super class is Object.

And there is no such thing as 2D or 3D array in Java. They are arrays of arrays (of arrays). And since an array is an object this explains why int[][][] is a subtype of Object[][].
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Yes and no.
Yes, Object[][] is a subtype of Object[] -- you can assign an Object[][] to an Object[], and instanceof also works this way.
No, Object[] is not a subclass of Object[], its super class is Object.

And there is no such thing as 2D or 3D array in Java. They are arrays of arrays (of arrays). And since an array is an object this explains why int[][][] is a subtype of Object[][].


What about that bold line?

And this is for array assignments, that means, We can't assign different dimensional arrays....
 
Henry Wong
author
Marshal
Pie
Posts: 21419
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:
Rob Prime wrote:Yes and no.
Yes, Object[][] is a subtype of Object[] -- you can assign an Object[][] to an Object[], and instanceof also works this way.
No, Object[][] is not a subclass of Object[], its super class is Object.

And there is no such thing as 2D or 3D array in Java. They are arrays of arrays (of arrays). And since an array is an object this explains why int[][][] is a subtype of Object[][].


What about that bold line?

And this is for array assignments, that means, We can't assign different dimensional arrays....


First, I fixed the quote (to what I think Rob meant to say).

Basically, Rob is reporting that it is really weird. If you do assignments, or check it with the instanceof operator, then a Object[][] IS-A Object[] which IS-A Object. And it also enjoys being able to be assigned as such. ie. you can assign a Object[][] object to a reference for Object[].

However, if you actually try to follow the hierarchy, using the reflection libraries, you will see that the array hierarchy is flat. Every array class has java.lang.Object as its direct superclass.

Henry
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:

The toString() method of the Object class prints the object type, followed by an @, followed by the identity hashcode in hex... so... [I@3e25a5 means array "[" of int "I" with hashcode 3e25a5.



If 3e25a5 is the hashcode of the array, then on running again why do I get another value at times? Isn't the hashcode supposed to be same for a particular text/object/variable/whatever?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinoth Kumar Kannan wrote:
If 3e25a5 is the hashcode of the array, then on running again why do I get another value at times? Isn't the hashcode supposed to be same for a particular text/object/variable/whatever?


If you don't override the hashCode() method, which is inherited from Object class, it'll return the memory address of the object.(Condition applies! )
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The identity hash code will be the same -- within the same JVM. If you restart it then all bets are off.
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:it'll return the memory address of the object.

Whoever gave you that false idea? The memory address is shielded from you by the JVM. The identity hash code may be based on the (original) memory location, but there are no guarantees about this.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:
Whoever gave you that false idea? The memory address is shielded from you by the JVM. The identity hash code may be based on the (original) memory location, but there are no guarantees about this.


That's why I add the Condition applies term, for a newbie, this is easy to understand the concept.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic