views:

1500

answers:

9

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?

+9  A: 

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.

Paul Tomblin
I agree; however (just to be a devil's advocate) there are people who are used to loosely-typed systems where they can just start using an undefined string variable and it be treated as empty.
Chris Jester-Young
Agreed, but there are reasons why you might wish to initialize your strings. It might be worth explaining to the poster the difference in the two.
Doomspork
@Chris: But Java is not a loosely-typed language so why even bother with a half-assed attempt to make it look like one?
Tundey
+1  A: 

I would avoid doing this, you need to know if your instances aren't getting populated with data correctly.

NSherwin
+16  A: 

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.

Yuliy
+1, but one caveat: if you're populating your objects from a database, and that database happens to be Oracle, then an empty string is treated as null. However, explicitly initializing to "" isn't going to help you in that case.
kdgregory
+1  A: 

Generally it would be best to avoid this. A couple of reasons:

  1. Getting a NullPointerException is generally a good warning that you are using a variable before you should be, or that you forgot to set it. Setting it to an empty string would get rid of the NullPointerException, but would likely cause a different (and harder to track down) bug further down the line in your program.

  2. There can be a valid difference between null and "". A null value usually indicates that no value was set or the value is unknown. An empty string indicates that it was deliberately set to be empty. Depending on your program, that subtle difference could be important.

Eric Petroelje
+2  A: 

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.

Michael Borgwardt
+1  A: 

No way. Why the hell do you want to do that. That will give incorrect results. nulls and """ are not same

vsingh
A: 

Null is better, that is why they are called unchecked exceptions {Null pointer exception}. When the exception is thrown, it tells you that you have to initialize it to some non null value before calling any methods on it.

If you do

private String field1 = "";

You are trying to supress the error. It is hard to find the bug, later.

vinaynag
A: 

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.

John O
A: 

I would suggest neither.

Instead you should give your fields sensible values. If they don't have to change, I would make them final.

public class Foo { 
    private final String field1; 
    private final String field2;
    public Foo(String field1, String field2) {
       this.field1 = field1;
       this.field2 = field2;
    }
    // etc. 
} 

No need to assign, i'm-not-initialised-yet values. Just give it the initial values.

Peter Lawrey