File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes what is the different Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what is the different " Watch "what is the different " New topic

what is the different

William Janet

Joined: Feb 10, 2011
Posts: 4
What is the difference between these two statements:

1) return foo.equals("abcde");

2) return "abcde".equals(foo);
Buddhika Mawella
Ranch Hand

Joined: Jan 06, 2011
Posts: 40

Think about a situation where variable foo has the value 'null'. when foo is null if you execute
that will create a NullPointerException.
but folowing code will never throw a NullPointerException

since "abcde".equals(null) always return Boolean 'false'.
hope you got it..

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 45255
Welcome to the Ranch

Please always tell us what the question is about.
I agree. Here's the link:
subject: what is the different
It's not a secret anymore!