Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Null is not an instanceof Object

 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
null can be cast to any object type without a ClassCastException.
 
Henry Wong
author
Marshal
Pie
Posts: 21413
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic