Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Default values problem.

 
Thomas West
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the 'ranch, by the way.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Thomas West
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone, that makes sense now. Still in the process of learning so I appreciate it very much.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic