File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Stefan Geelen
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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 ?
Stefan
 
John Dale
Ranch Hand
Posts: 399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Oops.]
Consider

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
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic