• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Arrays

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, everybody!
There is a question from Dan�s exam.
class E {
public static void main (String[] args) {
byte[] a = new byte[1];
long[] b = new long[1];
float[] c = new float[1];
Object[] d = new Object[1];
System.out.print(a[0]+","+b[0]+","+c[0]+","+d[0]);
}
}
What is the result of attempting to compile and run the above program?
Answer:
b. Prints 0,0,0.0,null
Explanation:
Each array contains the default value for its type. The default value of a primitive byte or a primitive long is printed as 0. The default value of a float primitive is printed as 0.0. The default value of an Object is null and is printed as null.

May be I am tired of studding but it seemed to me that the main() is just regular method and any variables declaring within this method are local and must be explicitly initialized upon declaration (they can not to be initialized here by default, because this is only for class members) .Where am I wrong?
Thanks.
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yelena,
Arrays are always initialized to default value, irrespective of being declared locally in a method or as an instance member of the class.
Hence the result.
Monisha.
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a difference between declaration and initialization.
If you declare a local array without initialization, as follows:

it results in compile time error.
The same for
This also results in compile time error.

But, the following is OK:

It compiles without error.
Variable references array object. Elements are given default value 0.

It also compiles without error.
Variable references array object. Elements are given null value.
Now, try the same on "class-level". You will get no compile time error, when declaring the arrays (without initialization).
[ April 03, 2003: Message edited by: Karin Paola Illuminate ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic