OCPJP 6
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?
When you do things right, people won't be sure you've done anything at all.
Subhash Kumar
Attitude is everything
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
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Abimaran Kugathasan wrote:
Henry, one question, which class you are mentioned here?
luck, db
There are no new questions, but there may be new answers.
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
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
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."
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
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.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
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[][].
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
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....
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
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.
OCPJP 6
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?
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Abimaran Kugathasan wrote:it'll return the memory address of the object.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
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.
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Are we home yet? Wait, did we forget the tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|