wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why variables  created in Stack are not initialized Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why variables  created in Stack are not initialized" Watch "Why variables  created in Stack are not initialized" New topic

Why variables created in Stack are not initialized

Sneha Agrawal

Joined: Mar 13, 2005
Posts: 2
Local array is given default values but local variables are not given. just that local array are objects stored on heap and local variables on stack makes the difference some one would say. But why variables on stack are not given default values why they are not initialised. why

look at the program below for those needing more explanation to my question.

[ March 13, 2005: Message edited by: Sneha Agrawal ]
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
This is an arbitrary decision by the designers. Every language is different.

One rationale: Many programmers group all the declarations at the beginning of a method and use them later or not at all. Allowing local variables to go uninitialized until used saves time in frequently called methods.

Mike Gershman
SCJP 1.4, SCWCD in process
Jon Lee
Ranch Hand

Joined: Mar 04, 2005
Posts: 134
When you declare a local variable like [int i], it is only a declaration, system does not allocate memory for the variable. So it is not initialized.

But for statements like "int ia[] = new int[3];", system allocate memory on the heap for the array. Since it has memory, you have to put something in it. You cannt leave the memory empty, right? So the array is initialized.
[ March 13, 2005: Message edited by: Jon Lee ]

SCJP 5.0 - 98% (2007)<br />SCWCD 1.4 - 97% (2007)
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Actually, memory is allocated on the stack for all the local variables in a method as soon as the method is entered. The method's local variables plus some save areas for returning to the calling method are allocated together as a "stack frame" when the method is called.

Java assigns default values (0 or null) to all instance variables early in the instance creation process. It was a conscious choice not to do the same thing for local variables.
Daniel Rhoades
Ranch Hand

Joined: Jun 30, 2004
Posts: 186
This just extends from Mike and Jon's comments

Because arrays are actually objects in Java, it makes sense that their elements are initialised.

int ia[] = new int[3];

You are actually asking the compiler to create an array to holding 3 int's, so as you can imagine it creates 3 primtives in memory with all the bits set to zero.

int i;

Here you are just declaring a reference, nothing has been created yet.

I see it as if the new operator has been used, then defaults must be used to initialise the array's state.

Drinking more tea is the key...
wood burning stoves
subject: Why variables created in Stack are not initialized
Similar Threads
Sun Cirtification
Local variable initialization - source: http://www.jchq.net/mockexams/exam1.htm
Final Variable Declaration
another thread question
variable declaration