Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Initializing reference variable

 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • 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 ?
 
Henry Wong
author
Marshal
Pie
Posts: 21116
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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 ?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • 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
  • 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 ?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I see.. understood.. so that is why one code compiles while the other does not.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic