aspose file tools
The moose likes Java in General and the fly likes Does sun spec guarantee default initialization for primitive arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Does sun spec guarantee default initialization for primitive arrays" Watch "Does sun spec guarantee default initialization for primitive arrays" New topic
Author

Does sun spec guarantee default initialization for primitive arrays

Abhinav Anand
Ranch Hand

Joined: May 02, 2002
Posts: 113
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
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24041
    
  13

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.


[Jess in Action][AskingGoodQuestions]
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel/download
 
subject: Does sun spec guarantee default initialization for primitive arrays
 
Similar Threads
Marcus Green Mock #3
Marcus exam 3 # 26
boolean practice question
Array
local arrays