This discussion is archived
1 2 Previous Next 15 Replies Latest reply: Apr 28, 2010 5:51 AM by dcminter RSS

Difference in Null and Empty String

800481 Newbie
Currently Being Moderated

I have been wondering about the difference between Null and Empty String in Java. So I wrote a small program like this:

public class CompareEmptyAndNullString {

     public static void main(String args[]) {

          String sNull = null;
          String sEmpty = "";
          try {
               if (sNull.equalsIgnoreCase(sEmpty)) {
                    System.out.println("Null and Empty Strings are Equal");
               } else {
                    System.out.println("Null and Empty Strings are Equal");
          } catch (Exception e) {

This program throws Exception: java.lang.NullPointerException
     at practice.programs.CompareEmptyAndNullString.main(

Now if I change the IF Clause to if (sEmpty.equalsIgnoreCase(sNull)) then the Program outputs this: Null and Empty Strings are Equal

Can anyone explain why this would happen ?

Thanks in Advance !!
1 2 Previous Next