Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is the different

 
William Janet
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between these two statements:

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

2) return "abcde".equals(foo);
 
Buddhika Mawella
Ranch Hand
Posts: 40
Eclipse IDE Java Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please always tell us what the question is about.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic