• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Default values problem.

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the 'ranch, by the way.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone, that makes sense now. Still in the process of learning so I appreciate it very much.
 
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic