• 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

Initializing reference variable

 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


this does not compile but



this compiles.

How does assigning null to abc make the code compilable ? What is the difference between declaring String abc and String abc=null ?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java, local variables must be initialized. The compiler will complain when they are not. And BTW, just because a program compiles does not mean that it is correct. If you run the program, it should throw a null pointer exception, as soon as it tries to use the variable.

Henry
 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,

That's right. Since "abc" is a local varibale, it must be initialized. hence the first code shows a compilation error while the 2nd code does not as it is assigned with a null value.

My question is since in the first code, "abc" is not initialized, doesn't it mean that "abc" points to null ? If that is true then the 2nd code should not compile as well as "abc" still points to null. Right ?
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arnb Sen:
Hi Henry,

That's right. Since "abc" is a local varibale, it must be initialized. hence the first code shows a compilation error while the 2nd code does not as it is assigned with a null value.

My question is since in the first code, "abc" is not initialized, doesn't it mean that "abc" points to null ? If that is true then the 2nd code should not compile as well as "abc" still points to null. Right ?



No, abc does not "point to null" in the first version because it is uninitialized. When you declare a local variable, the compiler will allocate some space in memory for it. However, this location in memory already has some data in it from the last time it was used. If Java allowed the code in your first example, then it would use this unknown value as the reference. This uknown value could be pointing to a supposed object just about anywhere in memory. Can you see why this is bad? So, abc will point to null, only if you explictly say so as in your second example.

Layne
 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok understood.. so if abc points to null, JVM removes the unknown data and replaces it with a null object. 'abc" then points to this null object. Right ?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not a null object -- null means precisely "no object at all." And "no object" is quite different from "some random value."
 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see.. understood.. so that is why one code compiles while the other does not.
 
money grubbing section goes here:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic