Why does Java provide a default value to instance variables and does not do so to local variables (variables declared in a method) ? Is there a difference in the way they are handled?
"I can resist everything except temptation"
posted 10 years ago
Instance variables represent the object on the heap while local variables are on the stack. To initialize local variables java would have to insert code at the top of every method to update those stack-resident variables, which means a memory write for every local variable. The cummulative effect would be very signficant and it's usually redundant: locals are always set before they are used.