• 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

I have doubt with for each

 
Ranch Hand
Posts: 246
Firefox Browser Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


1.if i don't give any {} in the array it fail compilation due to initializtion
2.when i give it how it is initialized and can array store "null" value ?
 
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
The size of an array is set when it's created, and cannot be changed later. When you say int[] a = {}; you are creating an array of length zero.

On the other hand, if you say int[]a = new int[5]; you are creating an array of length five. However, you have not provided any int values for the array's elements, so each of these will initialize to zero.

An array in Java can hold primitive values or object references. If the array is declared to hold references, then it can hold null references. For example, if you say Object[] a = new Object[5]; you are creating an array of length five. In this case, each of the array's elements will initialize to null.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic