File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Null is not an instanceof Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Null is not an instanceof Object" Watch "Null is not an instanceof Object" New topic

Null is not an instanceof Object

jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
We can return null in a method with an object reference return type.

public Button doStuff()


return null; // how does this work fine , if null is not an instanceof Object

Gokul Nanthakumar
Ranch Hand

Joined: May 11, 2010
Posts: 56
here null means, reference not pointing to any object in the heap. so for any type of object reference we can say that it is not pointing to any object. hope helps.
Sujeet Kumar Jaiswal

Joined: Mar 07, 2010
Posts: 17
NULL is a special literal of the null type.

1- It can be cast to any reference type, but not to any primitive type such as int or boolean.
2- It is impossible to cast to the null type or declare a variable of this type.
jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
null is an instanceof object gives false.

Then when an Button object is expected as a return type, how could we pass null.

I feel null is an object because of the code below ???

public class ListTest {

public static void main(String args[])
List lt = new ArrayList();
lt.add(null);// add method takes only an object.


I had asked this question previously but couldnt get any satisfactory answers.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20276

null can be cast to any object type without a ClassCastException.

How To Ask Questions How To Answer Questions
Henry Wong

Joined: Sep 28, 2004
Posts: 20535

I think the issue that the OP is having is trying to classify what is the type of "null" -- as it seems to have some contradictory behaviors.

IMHO, I think it is easier to think of null as a literal, that can be cast to any object type, can be assigned to any object type, but when used with the instanceof operation, it always return false. I don't think it is correct to think of it as an object (or not an object). I think it may be best to think of it as a literal that has special behaviors.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Note that "instanceof" is generally irrelevant to the question of whether code will compile, as "instanceof" is a runtime operation (true, the compiler will reject impossible uses of instanceof between incompatible types.) In the case of null, though, the key thing is, as has been pointed out, that you can legally cast null to any reference type, and code is compiled based only on reference types (or "compile-time types.") The runtime type of an object, or the presence or absence of an object at all, has nothing whatsoever to do with compilation.

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: Null is not an instanceof Object
It's not a secret anymore!