Should I set the initial java String values from null to ""?

87,319

Solution 1

I disagree with the other posters. Using the empty string is acceptable. I prefer to use it whenever possible.

In the great majority of cases, a null String and an empty String represent the exact same thing - unknown data. Whether you represent that with a null or an empty String is a matter of choice.

Solution 2

That way lies madness (usually). If you're running into a lot of null pointer problems, that's because you're trying to use them before actually populating them. Those null pointer problems are loud obnoxious warning sirens telling you where that use is, allowing you to then go in and fix the problem. If you just initially set them to empty, then you'll be risking using them instead of what you were actually expecting there.

Solution 3

Absolutely not. An empty string and a null string are entirely different things and you should not confuse them.

To explain further:

  • "null" means "I haven't initialized this variable, or it has no value"
  • "empty string" means "I know what the value is, it's empty".

As Yuliy already mentioned, if you're seeing a lot of null pointer exceptions, it's because you are expecting things to have values when they don't, or you're being sloppy about initializing things before you use them. In either case, you should take the time to program properly - make sure things that should have values have those values, and make sure that if you're accessing the values of things that might not have value, that you take that into account.

Solution 4

Does it actually make sense in a specific case for the value to be used before it is set somewhere else, and to behave as an empty String in that case? i.e. is an empty string actually a correct default value, and does it make sense to have a default value at all?

If the answer is yes, setting it to "" in the declaration is the right thing to do. If not, it's a recipe for making bugs harder to find and diagnose.

Solution 5

I know this is an old question but I wanted to point out the following:

  String s = null;

  s += "hello";
  System.out.println(s);// this will return nullhello

whereas

  String s = "";

  s += "hello";
  System.out.println(s); // this will return hello

obviously the really answer to this is that one should use StringBuffer rather than just concatenate strings but as we all know that for some code it is just simpler to concatenate.

Share:
87,319
Martlark
Author by

Martlark

Lead developer working in TypeScript and workflow automation.

Updated on July 09, 2022

Comments

  • Martlark
    Martlark almost 2 years

    Often I have a class as such:

    public class Foo
    {
    private String field1;
    private String field2;
    
    // etc etc etc
    }
    

    This makes the initial values of field1 and field2 equal to null. Would it be better to have all my String class fields as follows?

    public class Foo
    {
    private String field1 = "";
    private String field2 = "";
    
    // etc etc etc
    }
    

    Then, if I'm consistent with class definition I'd avoid a lot of null pointer problems. What are the problems with this approach?