-3

I got different simulation results when I programmed in these two ways:

if (S == null) {
        return new LinkedList<>();
    }

and

int len = S.length();
if(len == 0) return new LinkedList<>();

The first code gave me [""], which passed the testing. While the second one gave me [], got failed. And I also noticed that there is another way: S.isEmpty()

Would anyone please explain? Many thanks!

MiaT
  • 11
  • 2
  • 6

7 Answers7

5

String == null checks if the object is null (nothing, not even an empty string) and String#length() == 0 (actually, you should use String#isEmpty() instead) checks if the string object has 0 chars. Also, you can't access any methods if the object is null, it will throw a NullPointerException (or NPE for short).

Tassu
  • 215
  • 3
  • 13
4

difference between (string == null) and (string.length() == 0)?

Very different.

When you check (string == null), it checks whether the string reference is pointing to any existing object. If it is not referencing to any object, it will return true.

string.length() == 0 just checks your existing String object's content and see if its length is 0. If no object exist in the current variable when you invoke .length(), you get a NullPointerException.

user3437460
  • 17,253
  • 15
  • 58
  • 106
3

S is a reference variable (you should write it in lower case).

S (or rather s) references an object that provides the method length().

You can only access the object referenced by s, if s is a really a reference to an object. If s is null (s==null), s does not reference an object and therefore, you can not call the method length(). If you try, you will get a NullPointerException.

When s references an object, you can call the length method on that object. In this case, it is a string object. A string object may exist without any characters (empty string, or "").

  • String s; // just a reference, initial value is null
  • s = ""; // s now references an empty string and is no longer null
  • new String(""); // create a new object with an empty string

In Java, you never really work with objects. You only work with references to objects, though in most cases, it appears as if you work with the object directly.

Keep in mind that the reference variable and the object are really to different things.

coseos
  • 144
  • 1
  • 3
2

If the string you are passing into the second one is null, an exception should occur, since .length() will throw an exception when called on a null string.

HamishD
  • 349
  • 2
  • 15
1

if a String instance is null, myInstance.length() == 0 would throw a NullPointerException, because you call an instance member of a not instantiated instance and crash your application.

So, if you're not sure your String instance is instantiated, always do a null-check, or better yet, with Java 8 or later, use Optional to avoid null's.

Stultuske
  • 9,296
  • 1
  • 25
  • 37
1

S == null mean that there if you try to print something for instance, nothing wiil happen (or maybe a nullPointerEcxeption) because null mean that there is nothing inside this variable.

String.lenght(S) == 0 mean that your string equals to ''

for instance :

String S1 = '';
String S2 = null;
try{
  System.out.println(S1.length() == 0) {
  System.out.println('S1 is not null');
}catch(nullPointerExeption e){
  System.out.println('S1 is null');
}
try{
  System.out.println(S2.length())//it will throw you a java.nullpointerexcption
  System.out.println('S2 is not null');
}catch(nullPointerExeption e){
  System.out.println('S2 is null');
}

The system will write

0
S1 is not null

S2 is null
charles Lgn
  • 500
  • 2
  • 7
  • 28
-3
        //case1        
        String s;
            if(s==null)System.out.println("I am null");
            System.out.println(s.length());
     //error: variable s might not have been initialized
    //case2
    String s=null;
            if(s==null)System.out.println("I am null");
            System.out.println(s.length()); 
    /* output 
    I am null
    error: Null pointer exception
    */
    //case 3
    String s=new String();
            if(s==null)System.out.println("I am null");
            System.out.println("length is "+s.length());
    /* output 
    length is 0 
    */
    String s="";
            if(s==null)System.out.println("I am null");
            System.out.println("length is "+s.length());
    /* output 
    length is 0 
   */
  • 5
    ehm... no. s is not null in your example, s is instantiated. String s; here, s would be null. Also: there is a big difference between = and ==, you should read up on that. – Stultuske May 18 '18 at 06:18
  • 3
    @MiaT I see you've accepted this answer, I hope you realize it's wrong? – Stultuske May 22 '18 at 09:55