jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes obj != null and !(obj.equals(null)) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "obj != null and !(obj.equals(null))" Watch "obj != null and !(obj.equals(null))" New topic

obj != null and !(obj.equals(null))

Stefan Geelen
Ranch Hand

Joined: Nov 19, 2001
Posts: 49
Are obj != null and !obj.equals(null) the same ?
Sometimes I have the impression that obj != null doesn't work.
When I replace the code with !obj.equals(null) it works.
I have the impression that if the object is instantiated in the class itself I can use !=.
If I call method that returns an object I have to use obj.equals(null) to find out if the object is null, like in:
if (!pg.getRectangle.equals(null))
if (pg.getRextangle != null) doesn't seem to work correctly .
Anyone who can clarify this ?
John Dale
Ranch Hand

Joined: Feb 22, 2001
Posts: 399

The expression for A would return false.
The expression for B would give a null pointer exception.
If this does not get at what you mean, please give a concrete example.
[ April 27, 2002: Message edited by: John Dale ]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
What do you mean that "obj != null" doesn't work? If obj is null then "!obj.equals(null)" will throw a null pointer exception because you are trying to run a mthod against a null.

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: obj != null and !(obj.equals(null))
jQuery in Action, 3rd edition