This week's book giveaway is in the Spring forum.
We're giving away four copies of Modern frontends with htmx and have Wim Deblauwe on-line!
See this thread for details.
Win a copy of Modern frontends with htmx this week in the Spring forum!
  • 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

Regarding the Array Declaration

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

In K&b,chapter 3 age no:196.
Array element are always given default values,regardless of where the array itself is declared or instatiated.For me this line is confusing so i wrote a program



here i am getting a compile time exception saying array is not intialised
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An array is first 'declared' (i.e. declaring the reference only)
then it is 'constructed' that is an array object is created in heap.
At this point The elements of the constructed array are initialized to their default values.

At next step you can intialize/ set values for each element of the array.

in you sample code if you construct the array then you'll see that the element values are initialized to default 0 (for primitive int)

public class ArrayTest {

/** Creates a new instance of ArrayTest */
public ArrayTest() {
}
public static void main(String args[]){
int arr[]= new int[5];//declared here
for(int i=0;i<5;i++)
System.out.println("Array Values --"+arr[i]);
}

}
 
reply
    Bookmark Topic Watch Topic
  • New Topic