• 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

Variable initial values

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know if a boolean is declared but not initialized the value is 'false'. I also understand that other variables types, when not initialized, default to '0' or '/u0000' ect ...
My question is ... does this hold true for arrays also? i.e. Would:
Boolean b[] = new Boolean[2];
System.out.println(b[1]);
print out a 'false'?
thnx!
-rp
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since Boolean is an object it would get initialized to null. If it were boolean it would be set to false.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ryan
It depends on where the array is being used/declared. Arrays behave like any other object. As a class or instance variable the array variable itself will be initialized (but not its members) as a local variable the array variable will not be initialized until constructed.
An array as a class or instance variable:
int[] intArr; would initialize the variable intArr to null, all of the element of the array are uninitialized.
intArr = new int[4]; all elements of intArr have the default vaule of 0.
An array as a local variable:
int[] intArr; intArr is not initialized
intArr = new int[4]; all members of intArr are initialized to 0.
String[] stgArr = new String[2]; both elements of strArr are null, but strArr itself is not.
The only real difference is when the array is declared but not constructed on the same line. Other than that, after the array is actually constructed there is no difference in the way its members are initialized.
Dave
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dave, you said if the array is declared as class or instance variable it is initialzed by default as null, and if the array is declared as class or local variable it will not be initialzed. What I think is that arrayed always get initialized regardless of the scope or where they are declared.
--Farooq
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Muhammad,
If you declare an array reference variable as a class or instance variable; by default, it is set to null. If you declare it as a local variable ie inside a method; it will not be automatically set to null.
If you initialize an array using a class, instance or local variable, all the array elements will automatically be set to their default values.
Hope that helps.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Muhammad Farooq:
Dave, you said if the array is declared as class or instance variable it is initialzed by default as null, and if the array is declared as class or local variable it will not be initialzed. What I think is that arrayed always get initialized regardless of the scope or where they are declared.
--Farooq


Actually, this will genrate a compile error:

This won't:
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Muhammad
Check out this code:

classArr is declared as an instance variable so if it is declared but not initialized/constructed, the variable is initialized to null. The individual elements are not initialized, trying to access them gives NullPointerException.
classArr2 (also an instance variable) is declared and constructed. The variable refers to an address and all of the elements are initialized to their default.
stringArr is a local array that is declared but not constructed. Any reference to the variable will not compile because it hasn't been initialized yet.
intArr is a local array that is declared and constructed. The variable refers to an address and all of its elements have their default values.
The biggest difference is the way the actual array variable is handled. In a class or instance variable an uninitialized variable is always given its default. In the case of arrays the default is null.
In a local variable they are not initialized so do not get their default values and can't be accessed.
In an array, once it has been constructed with new then all of its elements are given their default values (unless given specific values).

hope that clears it up for you

Dave
I didn't notice that Jane and Thomas had both posted replys too - I type way too slow!!!

[This message has been edited by Dave Vick (edited July 31, 2001).]
 
straws are for suckers. tiny ads are for attractive people.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic