This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 24183
    
  34

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
Marijana Rukavina
Greenhorn

Joined: May 21, 2014
Posts: 3
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?


This is usually done because when typing null == abc you are forced to put == and not = (because null is not a reference), in case abc were of type Boolean. This is in order to prevent those kind of logically not intended bugs. I suppose the same logic applies here (null != abc).
Personally I don't like it, but some people use it.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

Hi Marijana! Welcome to the Ranch!

Thanks for contributing, but this thread is over 10 years old - I doubt they're still looking for an answer .
Marijana Rukavina
Greenhorn

Joined: May 21, 2014
Posts: 3
Hi Matthew,

I thought it might be useful for other people how stumble upon it during the search, as I did just now
But, if it is not the forum policy, then I wouldn't do this again
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
That sort of construct is hardly ever necessary in Java®, although it is in C/C++, where null can be represented as a 0. Boolean with a big B is the only type you can use there without suffering a compiler errorIf obj is declared to be any other type than Boolean, you will suffer a compiler error
NullDemo.java:6: error: incompatible types: Object cannot be converted to boolean
You would usually use a boolean (small b) after if. If you manage to execute the code with obj declared as a Boolean you will suffer a NullPointerException from unboxing.

And welcome again
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
Your post has enabled us to have some useful discussion, so please continue. We don't mind old threads being reopened as long as people realise they are old threads.
I cannot see any point in writing if (null != obj)… because nobody will write = instead of !=
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
A minute ago, I wrote: . . . nobody will write = instead of !=
Before Winston sees that post, can I add the myself
Marijana Rukavina
Greenhorn

Joined: May 21, 2014
Posts: 3
I cannot see any point in writing if (null != obj)… because nobody will write = instead of !=


There is no point, but regarding the previous thing I talked about (null == obj), where obj is of type Boolean, this (null != obj) would be just a consequence of that (in order to maintain, let's say, coding style)
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7549
    
  18

Campbell Ritchie wrote:Before Winston sees that post, can I add the myself

Sarky hound!

But you're absolutely right...

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
Winston Gutkowski wrote: . . .
Sarky hound!

But you're absolutely right...

Winston
Agree to both bits!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Type Casting a null object?
 
Similar Threads
result of invoke method
[newbie] invoking methods within a class (java.lang.reflect)
Proxying Objects Causes OutOfMemoryError
From Applied Exam
Try Catch finally