aspose file tools*
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Null is not an instanceof Object" Watch "Null is not an instanceof Object" New topic
Author

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
Greenhorn

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
Sheriff

Joined: Oct 27, 2005
Posts: 19557
    
  16

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18141
    
  39


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.

Henry


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

Joined: Jul 08, 2003
Posts: 24168
    
  30

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]
 
Don't get me started about those stupid light bulbs.
 
subject: Null is not an instanceof Object
 
Similar Threads
Passing dynamically created arguments to method
null & instanceof
equals(Object) of String
NullPointerException when removing objects, for which equals() is overriden, from JComboBox
null instanceof an Object ?