Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Type Casting a null object?

 
Vinu krish
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marijana Rukavina
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48954
60
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48954
60
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A minute ago, I wrote: . . . nobody will write = instead of !=
Before Winston sees that post, can I add the myself
 
Marijana Rukavina
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Before Winston sees that post, can I add the myself

Sarky hound!

But you're absolutely right...

Winston
 
Campbell Ritchie
Sheriff
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . .
Sarky hound!

But you're absolutely right...

Winston
Agree to both bits!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic