• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

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

 
Ranch Hand
Posts: 49
  • 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
 
Ranch Hand
Posts: 399
  • 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 ]
 
mister krabs
Posts: 13974
  • 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.
 
A "dutch baby" is not a baby. But this tiny ad is baby sized:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!