The Artima Developer Community
Sponsored Link

Java Answers Forum
Casting a null object is not error prone?

3 replies on 1 page. Most recent reply: Feb 26, 2002 1:50 PM by Alain Ravet

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
jinaraj

Posts: 8
Nickname: bullet
Registered: Feb, 2002

Casting a null object is not error prone? Posted: Feb 26, 2002 7:03 AM
Reply to this message Reply
Advertisement
Please have a look at this code

Object ob = null;
String s = (String)ob;

I was under the impression that java will throw a NullPointerException at line 2. But it is not happening. Could you please tell me why it is not throwing an exception, as I expected?


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Casting a null object is not error prone? Posted: Feb 26, 2002 8:25 AM
Reply to this message Reply
The second line is setting your String s to a null string object.

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Casting a null object is not error prone? Posted: Feb 26, 2002 9:02 AM
Reply to this message Reply
null is a literal of type null which is only avaialable as in literal form. I mean to say is there is a reference type in java , say Null just for the sake of discussion, which is not available to programmer for creating object of its type. There is only one literal value null available of its type. The speciality that is attached to this type is that it is assignable to all other reference type in Java. When you cast an object reference then it checks that whether the object reference is assignment compatible to the reference type you are casting to. For example, when you write
String str = (String)null ; then it checks if null can be assigned to String.
So, in your case it checks if ob can be assigned to a String object reference. Since at the time of casting ob contains a null reference and null reference can be assigned to any reference variable in Java , you don't get any error.

Thanks
Kishori

Alain Ravet

Posts: 19
Nickname: aravet
Registered: Feb, 2002

Re: Casting a null object is not error prone? Posted: Feb 26, 2002 1:50 PM
Reply to this message Reply
public void testNullString ()
    {
       String realString = "qljf";
       String nullString = null ;
 
        testI(realString, "error 1");
        testI(null      , "error 2");
        testI(nullString, "error 3");
    }
    private static void testI(final String testee, final String msg) {
        if ( !(testee instanceof String)) {
            System.out.println(testee + " is not an instaceof String.  " + msg);
        }
    }


will produce :

> null is not an instaceof String. error 2
> null is not an instaceof String. error 3



Advantage:
When overwriting equals(), once you have tested for instanceof, you don't have to test for null ;

class Person (){
 
   public boolean equals(Object o) {
      if (!(o instanceof Person)) return false ;
      
      Person obj = (Person)o ; // obj cannot be null
 
      return obj.__fName.equals (this.__fName) ;
   }
 
   private String __fName ;
}
 
 

Flat View: This topic has 3 replies on 1 page
Topic: an servlet example  works with msaccess... Previous Topic   Next Topic Topic: Staroffice document thru Java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use