1 2 Previous Next 15 Replies Latest reply on Apr 28, 2010 12:51 PM by dcminter

    Difference in Null and Empty String


      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(CompareEmptyAndNullString.java:10)

      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