1

I'm newbie in programming so please bear with me. I would like to know what does set null do? At the 3rd line a and b are pointing to the same memory location after setting one of them to null the other one is still pointing to the old location.

Object a,b;
a = new Object();
b = a;
print(a == b); // true
a = null;
print(a == b); //false

Can someone explain me What does exactly set to null do? Is null another location in memory?

Craig Curtis
  • 853
  • 10
  • 24
StanfordK
  • 64
  • 7
  • 1
    *"Is null another location in memory"* - No, it's a special construct which means "points to nothing/no where" - you might also like to have a look at [this previous question](https://stackoverflow.com/questions/2707322/what-is-null-in-java) – MadProgrammer Oct 03 '17 at 00:29
  • 1
    Also duplicate of: https://stackoverflow.com/questions/2028298/what-is-null-in-c-java – Rufus L Oct 03 '17 at 00:32

1 Answers1

0

Null is nothing, nonexistence, the absence of beingness, the path to a nether world down which any attempt to dereference brings one to the brink of insanity.

Simply speaking, null means that the reference or pointer is literally pointed to nothing at all. It is distinct from zero values such as 0, 0.0, '0', '\0' (the "null character"), and "" (the empty string) as well as objects that have been newly instantiated and have not been modified from their default state. Collection objects that have been instantiated but contain zero elements are not null, they are empty objects. A null reference is not an object at all, but the absence of one.

Any attempt to dereference a null will result in an exception or a crash.

Robert Columbia
  • 6,313
  • 15
  • 32
  • 40