• Post Reply Bookmark Topic Watch Topic
  • New Topic

initialisation and default values  RSS feed

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question related to q44 of Examlab.


Why does the above code not compile although the statement String i should lead to an initialisation of i to the value of null which is the default value for Strings.

Many thanks in advance!
JG

}
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i is a local variable and as such is not initialized to a default value. Only member variables, those declared outside of a method, are given default values. You must explicitly assign a value to a local variable before you use it, otherwise you will get a compile-time error.
 
Jo Gupta
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:i is a local variable and as such is not initialized to a default value. Only member variables, those declared outside of a method, are given default values. You must explicitly assign a value to a local variable before you use it, otherwise you will get a compile-time error.


Got it. Very helpful! Thanks!
JG
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

Fields are created in objects on the heap. The JVM can clear the memory before the objects are created, and initialises fields to default values.
Local variables are on the stack; the stack is not routinely cleared, but all memory is overwritten as the methods are executed. So there is a risk that an uninitialised local variable would occupy space previously used, and there would be an erroneous value in that memory. The compiler therefore enforces rules that require you to initialise all local variables before use. Local variables must be definitely assigned (you can read about it in the Java® Language Specification (=JLS) but that is sometimes difficult to read. This JLS section is probably relevant too.
 
Jo Gupta
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome again

Fields are created in objects on the heap. The JVM can clear the memory before the objects are created, and initialises fields to default values.
Local variables are on the stack; the stack is not routinely cleared, but all memory is overwritten as the methods are executed. So there is a risk that an uninitialised local variable would occupy space previously used, and there would be an erroneous value in that memory. The compiler therefore enforces rules that require you to initialise all local variables before use. Local variables must be definitely assigned (you can read about it in the Java® Language Specification (=JLS) but that is sometimes difficult to read. This JLS section is probably relevant too.


It makes perfect sense. Thanks for the additional explanation!
JG
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!