Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are local variables also called "Automatic" in Java?

I read this in Kathy Sierra's book:

"Local variables are sometimes called stack, temporary, automatic, or method variables, but the rules for these variables are the same regardless of what you call them."

Why are the local variables called automatic?

like image 659
unj2 Avatar asked Apr 20 '10 03:04

unj2


People also ask

Are local variables automatic?

In computer programming, an automatic variable is a local variable which is allocated and deallocated automatically when program flow enters and leaves the variable's scope. The scope is the lexical context, particularly the function or block in which a variable is defined.

Are local variables automatically initialized?

The Java standard demands that every local variable must be explicitly initialized before being used. This differs from instance variables, which are implicitly initialized with default values (which are 0 for numbers and null for objects).

What is the difference between auto and local variable?

auto variables (not to be confused with auto keyword) are typically non-static local variables. They are stored in what is usually called "stack" space. "Local variables are non existent in the memory after the function termination", You probably mean Scope, { , } and not function termination.

What is called local variable in Java?

Local variables are declared in methods, constructors, or blocks. Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor, or block. Access modifiers cannot be used for local variables.


2 Answers

Local variables automatically cease to exist when the execution of the block in which they are declared completes.

 {
   int a = some_initialisation_value;
   ....
 }
 // a automatically vanishes here.
like image 164
codaddict Avatar answered Oct 12 '22 01:10

codaddict


Good ol' Wikipedia

In computer programming, an automatic variable is a lexically-scoped variable which is allocated and de-allocated automatically when program flow enters and leaves the variable's scope. The term local variable is usually synonymous with automatic variable, since these are the same thing in many programming languages.

like image 33
mob Avatar answered Oct 12 '22 03:10

mob