• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

why a local array which is an array initializes to 0 ?

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in case of local variables, they must be initialised or error occurs. But in case of array variable it initialises automatically to zero. How and Why? i was trying out this code expecting an error. but the values in a[] are initialised to '0'.
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because arrays in java are objects. and an object must have valid initialization state if you don't explicitly initialize it.

for e.g we have a Dog class and your create new Dog object in main method

Dog d = new Dog(); // here d is a local variable but it has been initialized to new Dog. so when Dog object gets created and assuming that you have not provided any constructor the various dog fields will be initialized to their respective defaults.

same is with int array.

int[] a = new int[3]; // a is of the type int array which points to integer array object that acts as a container with 3 partitions and those partitions are initialized to 0 which is default value of int.

if you have Dog[] d = new Dog[3] // this would result in a dog array which would have been initialized to null values rather than any garbage values
 
midhuna peru
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok so like the instance variables of class dog are initialised through dog() default constructor, the values in array object are initialized automatically. but is this a work of default constructor in this array case? whats the constructor name? is it a?
 
Marshal
Posts: 27371
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To rephrase what gurpeet singh said:

You have this declaration with assignment:



Here the variable "a" is declared, and it is initialized. It is initialized by assigning that array with three elements to it.

So that's it for the variable "a", it's initialized to an array. But you're asking about the contents of the array, which is a separate question.

Array elements are always initialized to zero, if they are primitives, or to null if they are object references. So since this is an array of primitives, the array elements are all initialized to zero.
 
midhuna peru
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, now i got this.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Java Language Specification ofcourse explains how this works:

JLS 10.6 wrote:
The length of the array to be constructed is equal to the number of variable initializers immediately enclosed by the braces of the array initializer. Space is allocated for a new array of that length. If there is insufficient space to allocate the array, evaluation of the array initializer completes abruptly by throwing an OutOfMemoryError. Otherwise, a one-dimensional array is created of the specified length, and each component of the array is initialized to its default value (ยง4.12.5).


For the type int, the default value is 0, as you can read in 4.12.5 Initial Values of Variables.
 
And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic