Win a copy of Svelte and Sapper in Action this week in the JavaScript 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

default value for arrays?

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the following statement about array right?
Only uninitialized class variables get default value automatically assigned to them. Local variables never get default values automically assigned. The exception is arrays, they always get default values assigned no matter where they are defined.
I declared an array in the main function, but the compiler gives an error saying that the array is not initialized.
thanks.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi jordan,
If you declare an array reference variable in a method it is not given a default value but when you initialize an array in a method, the component elements are set to the default value for their type.

Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a valid statement.
Where ever the array is defined, wheather class, method or static main method, they GET DEFAULT VALUES ASSIGNED.
Like Jane, mentioned, they get the default values
based on the arrays data type.
However in my opinion, since the array reference are nothing
but a unique bit pattern, initialization of the reference shouldnt be a concern...
Thankx
 
Enthuware Software Support
Posts: 4467
42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>Where ever the array is defined, wheather class, method or static main method, they GET DEFAULT VALUES ASSIGNED
No! Nothing gets intialized to default values if declared in a method.
Arrays are objects and default value of an array reference is null.

-Paul.
------------------
SCJP2 Resources, Free Question A Day, Mock Exam Results and More!
www.jdiscuss.com
Get Certified, Guaranteed!
www.enthuware.com/jqplus

Your guide to SCJD exam!
www.enthuware.com/jdevplus
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just my little addition to try to round it all up: (Get it? 'round up'?)
As Paul said, an array declared as a class or instance variable will be initialized to null. That is the value of the array variable not the values of the elements of the array.
In a method the array variable, just like all other variables, is not initialized. However, with an array, when you actually use new to construct it, then its individual elements are assigned their default values.
In a sense arrays behave the same as other variables regarding initialization and default values depending on where they are created. It is the members of the array that behave slightly differently (or contrary to what you would expect) when the array is actually allocated in a method.
hope that summed everything up for you
Dave
[This message has been edited by Dave Vick (edited July 24, 2001).]
 
Get out of my mind! Look! A tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic