This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Default values problem. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Default values problem." Watch "Default values problem." New topic
Author

Default values problem.

Thomas West
Greenhorn

Joined: Dec 05, 2005
Posts: 5
I know what the default values are for the various types but this one has me confused.



It appears that the array is displaying garbage. Why isn't the expected default value of 0 (zero) being printed?
Thomas West
Greenhorn

Joined: Dec 05, 2005
Posts: 5
Actually I caught what I did wrong.....I forgot ai[0].

That brings up another question though. Why does it not cause an error or warning when printing the array to the console without an index and display what appears to be random garbage at that location?
[ December 30, 2005: Message edited by: Thomas West ]
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
That ain't random garbage; that's just what you asked for. Remember, when you have a name for an object, such as ai (arrays are objects), you really only have a pointer to that object. You printed out the pointer: the exact location in memory at which I can find the array ai.


I've heard it takes forever to grow a woman from the ground
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
Welcome to the 'ranch, by the way.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

What you get when printing an object reference depends on whether that object's toString method is overridden.

In the case of an int array, toString returns the type as "[I", followed by "@" and then a hexadecimal representation of the hashCode, which in this case is a memory address (i.e., the same as Object).

You can see this by running the following code...


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Thomas West
Greenhorn

Joined: Dec 05, 2005
Posts: 5
Thanks everyone, that makes sense now. Still in the process of learning so I appreciate it very much.
 
GeeCON Prague 2014
 
subject: Default values problem.