aspose file tools*
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


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 "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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Default values problem.
 
Similar Threads
problem with clone.......
Char default value - Enthuware question
Array
JQplus
What is the Initial value of char literal?