The distinction between "null" and "non-initialized" is more clear in other languages like C++. Java is usually a safe enough language where you don't have to worry about it, except in this specific case.
In almost all programming languages (Java included), variables do not store the value of the object declared inside them. If you declare that "String s = "abc"", the String variable s does not store "abc". Rather it stores an integer which points to a location in memory where the actual string "abc" is stored. On the other hand, if a variable is set to null, that variable stores the number 0. Zero is a pointer to the "null address" of memory.
In Java, most of this is abstracted away from you. There's no way to get the actual pointer value of a variable. However it's still important to understand in order to know how objects are going to perform. If you create an object like this:
You have only created one object, but two references to that object.
Now why does this matter for the topic at hand?
Well, if you make an uninitialized variable:
You might imagine that s is null, but it is not. You didn't set s. It's not a pointer to memory address 0 (null), it's actual nothing at all.
Java tries to be a really safe language so you can't mess yourself up with this. That's why it forces you to initialize variables. However in C++ and lower level languages, you CAN use variables when they are not initialized, and they aren't null. You can either crash your program or get data from some other place in memory. That's why this is important. Java also does try to be safe by usually automatically initializing variables to null or some other default if you don't.
Paul Clapham wrote:Instance and class variables, on the other hand, don't need to be initialized and they default to zero or null if you don't initialize them.
Actually, in Java, all variables are initialized. But if they are not explicitly initialized, they default to implicit initialization of 0 for arithmetic values or null for objects. This was a conscious design decision on the part of Gosling and his team because Java was intended to be secure and reliable and one of the biggest sources of hard-to-detect bugs in C and C++ was uninitialized variables, since they were effectively initialized all right, just with whatever random value had been living in their alloted RAM location.
Campbell Ritchie wrote:Is there such a thing as a null address, or does the runtime simply interpret 0 as meaning null?
There is no such thing as a "null address" in Java, because Java does not support pointers. Object references are not the same either theoretically nor in many cases, in practice.
But yes, in C and C++ "0" is officially designated as the null pointer value even on hardware where there is no concept of memory location zero.
"privilege" comes from the Latin words for "private" and "law" (legal) and dates to feudal times. To "claim privilege" meant that you were above the laws that applied to the common people.