• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Printing a primitive array

 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
iterating through an array is another way to output.

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To print an array nicely formatted, you can also use Arrays.toString:

 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Marshal
Posts: 75664
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13048
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 75664
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Henry has explained it far better than I would have.
 
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I didn't say it. I'm just telling you what this tiny ad said.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic