Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Null

 
Doit
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can some one please list out different points to remebre about NULL??
ex: can it be compared with a string?
What happens when we use it in RHS of instanceof operator??
can we use it in equals()
etc
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should probably write few lines of code trying different combinations. This way, you will never forget. If you find anything peculiar or dont understand why, then post it here.
1. null can be assigned to any object reference variable.
2. null can be compared with any object reference variable.
3. null can not be used in equals, since the signature of the equals method is
boolean equals(Object obj)
null is not an object, it is a literal.
4 null can not be used as a right hand side operator of instanceof. Allowed are, Class,Interface, Array.
for example:

int a[]=new int[7];
System.out.println(a instanceof int[]);


If you need to refer to the api, here it is :
http://java.sun.com/products/jdk/1.2/docs/api/index.html

[This message has been edited by vasansrini (edited August 05, 2000).]
 
Doit
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vasansrini,
your 3th point which is null can not be used in equals is NOT CORRECT.
'null' can be used as an argument for the equals(Object obj) method which is inherited to all classes from 'Object' class.The equals(...) method takes an object reference variable of type 'Object' class and the value 'null' can very well be substituted for any object reference parameter.
If we look closly all your 3 points your 1st and 3rd points are contradicting. Isn't?. The following code is legal and will compile and run without any problem with the output of 'false'.
<pre>

class Test{
public static void main(String [] args){
System.out.println("Maha".equals(null));
}
}
</pre>
Output:
false


[This message has been edited by maha anna (edited August 05, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Wrong Assumption. I should have tried some code.
point to note:
When we use equals() to check whether the object reference is null, if the reference is null, it will throw a NullPointerException.
[This message has been edited by vasansrini (edited August 05, 2000).]
 
Suresh Chidambaram
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While on this topic
Assuming s is a String reference, and initalised,
IS s+ null == s ??
------------------
Romba Chinna Thambi
 
Doit
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are some
null== null ------ True
null== "" ------ False
"" == "" ------ True
String+null == String ----- false
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic