File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Type Casting a null object??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Type Casting a null object??? " Watch "Type Casting a null object??? " New topic
Author

Type Casting a null object???

Vinu krish
Greenhorn

Joined: Nov 20, 2003
Posts: 16
Hello..
I was doing a code review and found out that they have typecasted a null object. I was under the impression that it will throw a null pointer exception.But it didnt. Wht is the use of doing this?
This is the code
public void methodName(UserDefinedObject obj)
{
.......
}
They have used the following code to invoke methodName
methodName((UserDefinedObject) null);
A null can be passed directly instead of type casting. Does this have any performance issues?
I also saw another style of coding. I normally use the following
if(abc!=null). But I came across the following style if(null!=abc). Again any specific reason for doing this this or is it only a coding style?
C. Nimo
Ranch Hand

Joined: Mar 23, 2004
Posts: 82
Hi.
There's no need to do the typecasting if you're passing a null, since a null is valid for any object (in a sense).
The compiler will give you the same bytecode in both ways, though, since this is cought at compile-time and not at runtime (the null is explicit).
So - no performance issues here.
Regarding the second question - it doesn't matter. It's only a coding style (I'd say - not a very nice style, but I never argue about coding or coffee).
Nimo.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

The reason for the typecast may be that methodName() is overloaded. If you pass null to an overloaded method, the compiler may not be able to decide which version you want and so won't compile the code. The cast tells the compiler which overload to invoke.


[Jess in Action][AskingGoodQuestions]
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
I, too, had the similar impression that a NullPpointerException will most definitely be thrown, but it wasn't.
Then in Java forum, I was pointed to the fact that the JLS specify so.


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Vinu krish:
I also saw another style of coding. I normally use the following
if(abc!=null). But I came across the following style if(null!=abc). Again any specific reason for doing this this or is it only a coding style?

I see no reason to do this. I have seen something similar with Strings that is a good idea. Instead of doing this:
if (myString.equals("ABC"))
do this:
if ("ABC".equals(myString))
The first can cause a NullPointerException. The second can't.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Type Casting a null object???
 
Similar Threads
From Applied Exam
Type of null? Help???
how to create a null type
result of invoke method
examlab - 1 [reference variable casting]