Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is null not an object in Java, if it can be assigned to any reference type? [closed]

Tags:

java

My first question is does the null literal have a reference type? I am confused on if null has no reference type, or the null literal does have a data type, but there is no name for it. Either way as a literal, null can be of any reference type. If null can be assigned to any Object type, why can null not be an object itself? Even a String literal is an object itself in Java. Every reference type is able to make an Object in Java, then should not the same be applied to null?

like image 438
lisapizza Avatar asked Jul 19 '20 22:07

lisapizza


People also ask

Can object be assigned null in Java?

In Java, a null value can be assigned to an object reference of any type to indicate that it points to nothing. The compiler assigns null to any uninitialized static and instance members of reference type.

What happens when we assign null to object in Java?

If you pass it to a thread to be manipulated, the thread will have a reference to the object until it terminates. In all of these cases, if you set list = null , the references will still be maintained, but they will disappear after these references disappear.

Can an object reference be null?

One of the main causes of bugs with null reference is the fact that in C every reference type object can be null, all the time.

Can a reference variable be assigned as null?

Null Assigned to any Reference Variable It would be awful if each class had its own special value to show that no object of that class was present. We need a universal value that means "nothing here". The value null works for this and can be assigned to any reference variable.


1 Answers

Unfortunately, in many tutorials, books and other resources about Java, the terms "object" and "variable" are used in a sloppy way, which can lead to confusion similar to what you describe in your question.

For example, look at this line of code:

String message = "Hello World";

People will often say "I have a String object named message here with the value "Hello World". This is wrong and it makes beginners think that objects and variables are the same thing, and that objects have names.

Accurate would be: "I have a variable named message here which is a reference to a String object with the value "Hello World".

Variables and objects are not the same thing. A variable* is not an object, it's a reference to an object (it's a pointer to an object somewhere in memory).

Variables have names. In the code above, there is a variable named message. Objects do not have names.

There is also not a one-to-one correspondence between variables and objects. Multiple variables may refer to the same object.

The value null is a special value that you can assign to variables* that means "this variable refers to no object". It's just like a null pointer in languages such as C and C++.

It doesn't really make sense to talk about null being an object. It's not an object, it's a special reference value to make variables refer to "no object".

If null can be assigned to any Object type

This is why I suspect you're confused about the difference between objects and variables: you do not assign null to an object, but to a variable (of "any object type" which is the same as "of a non-primitive type").

*: we're talking about variables of non-primitive types here

For a more advanced understanding:

The value null has a bit of a bad reputation (it's the cause of many bugs). You might want to use java.util.Optional to help prevent some of the problems that null causes.

See also the null object pattern, which is a design pattern about the idea of having a special object that represents "the empty value". This might be useful in special situations.

like image 74
Jesper Avatar answered Sep 27 '22 20:09

Jesper