File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Printing a primitive array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Printing a primitive array" Watch "Printing a primitive array" New topic
Author

Printing a primitive array

Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

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?


OCPJP 6
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1613
    
  10

iterating through an array is another way to output.


When you do things right, people won't be sure you've done anything at all.
subhash kumar
Ranch Hand

Joined: Jul 14, 2010
Posts: 63


Subhash Kumar
Attitude is everything
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13869
    
  10

To print an array nicely formatted, you can also use Arrays.toString:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4522
    
    5

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


luck, db
There are no new questions, but there may be new answers.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Oct 13, 2005
Posts: 36478
    
  16
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

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Oct 02, 2003
Posts: 10911
    
  12

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

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

Joined: Oct 13, 2005
Posts: 36478
    
  16
I think Henry has explained it far better than I would have.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Oct 27, 2005
Posts: 19541
    
  16

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

Joined: Nov 04, 2009
Posts: 2066

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
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

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

Joined: Oct 27, 2005
Posts: 19541
    
  16

Exactly.
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

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

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Oct 27, 2005
Posts: 19541
    
  16

The identity hash code will be the same -- within the same JVM. If you restart it then all bets are off.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

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

Joined: Nov 04, 2009
Posts: 2066

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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Printing a primitive array
 
Similar Threads
arrays?
local arrays
enhanced for loop problem -- from a mock
Array problem
Cast Object to int[]