• 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

Does sun spec guarantee default initialization for primitive arrays

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

As far as i know about java for all these years. The following rule applies for arrays:

If the elements in an array are not purposely initialized when the array is created, the array elements will be automatically initialized with default values. The default values are:

* All reference types: null
* Primitive numeric types: 0
* Primitive boolean type: false
* Primitive char type: the Unicode character with 16 zero-valued bits ('\u0000' (the NUL character)

I have to prove to my client (he is in automation field) that a primitive array is by default initialized with 0. Please tell me where in the java specification is this behavior guaranteed.

Thanks in advance,
Regards,
Abhinav
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Section 15.10.1 of the third edition:


Then, if a single DimExpr appears, a single-dimensional array is created of the specified length, and each component of the array is initialized to its default value (�4.12.5).

If an array creation expression contains N DimExpr expressions, then it effectively executes a set of nested loops of depth N-1 to create the implied arrays of arrays.

 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you create an array it is guaranteed to be initialized regardless of the type or where you instantiate it: local, static, instance. Of course, that's not true of the array reference itself but we knew that.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic