File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why variables created in Stack are not initialized

 
Sneha Agrawal
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jon Lee
Ranch Hand
Posts: 134
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Mike Gershman
Ranch Hand
Posts: 1272
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 186
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic