This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Null" Watch "Null" New topic
Author

Null

Doit
Ranch Hand

Joined: Aug 03, 2000
Posts: 169
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Aug 03, 2000
Posts: 169
Thank you
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Aug 05, 2000
Posts: 20
While on this topic
Assuming s is a String reference, and initalised,
IS s+ null == s ??
------------------
Romba Chinna Thambi


Romba Chinna Thambi
Doit
Ranch Hand

Joined: Aug 03, 2000
Posts: 169
Here are some
null== null ------ True
null== "" ------ False
"" == "" ------ True
String+null == String ----- false
 
 
subject: Null