• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

initialisation and default values

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic