aspose file tools*
The moose likes Beginning Java and the fly likes difference between  null == object and object == null    Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "difference between  null == object and object == null    " Watch "difference between  null == object and object == null    " New topic
Author

difference between null == object and object == null

Selva Prakash
Ranch Hand

Joined: Sep 03, 2009
Posts: 41
can anybody explain which one is best null == object or object == null?. and why?
Thiagu Sin
Greenhorn

Joined: Jan 10, 2009
Posts: 13
object == null
Which returns a boolean value to check if you have defined a value for the variable object. If there is no value defined for the object variable than it is going to return true



Regards,
-Thiagu-
Selva Prakash
Ranch Hand

Joined: Sep 03, 2009
Posts: 41
ya. thant fine.. my question is any difference is there, if we place the null in right or left side while checking?
Thiagu Sin
Greenhorn

Joined: Jan 10, 2009
Posts: 13
Selva Prakash wrote:ya. thant fine.. my question is any difference is there, if we place the null in right or left side while checking?


Nope, There is no difference Selva
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

== is associative, which means that for any values "a" and "b", "a == b" will always give the same result as "b == a".

The "null == object" syntax is inherited from C programmers. In C, everything can be used as a boolean. A pointer is true if set, or false if NULL (the C equivalent of Java's null). Sometimes people made typing mistakes, leaving out one of the = signs: This is an assignment to object followed by an evaluation. Instead of checking if object was NULL this actually set object to NULL and then evaluated this, which would always yield false. The compiler didn't give any error because it's perfectly legal in C. (Although some compilers could give warnings.)

Now you know the back story you can forget about it. In Java this is possible in only a very few cases. All other cases will result in a compiler error. These cases all involve boolean or (since Java 5.0) Boolean. They are:
bool1 gets the same value as bool2, after which its value is evaluated.
Only possible if bool is a Boolean. Again the assignment, then bool is auto-unboxed. This will actually result in a NullPointerException since unboxing null is not possible.

The first one you can't prevent by swapping bool1 and bool2. The second one can be prevented by swapping bool and null; "null = bool" will always give a compiler error. That being the case, I never do this. I find "object == null" just a bit more natural than "null == object". And this one occurrence with Boolean can be found out through proper testing. The NullPointerException will help you out with that.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Rob,Excellent explanation
Selva Prakash
Ranch Hand

Joined: Sep 03, 2009
Posts: 41
wow .. good explanation.. thank you
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference between null == object and object == null