13

What is the best practice for initializing a String method (local) variable to avoid the "Variable might not have been initialized" error in Java?

String s=null; or String s="";

Does it make a difference? If so, which one is better and why?

I've read conflicting answers on the web.

Thanks!

1
  • Thanks for all your answers. Here's more info... I designed a web service method that returns a String. Based on certain conditions, that String is assigned a value, but not always. Should I return null or "" when that happens? Commented Mar 30, 2011 at 12:37

6 Answers 6

16

Yes, it makes a difference. One is a reference to an empty string, one is a null reference. They're not the same.

Which is better? It depends whether you want a null value or a reference to an empty string... they behave differently, so pick the one which behaves the way you want it to.

I rarely need to assign a "dummy" value to a variable though - usually just initializing the variable at the point of declaration with the real value I want is fine. If you can give us more context, we may be able to help you structure your code better so this isn't a problem for you.

Sign up to request clarification or add additional context in comments.

1 Comment

Would you be able to provide a situation where each of them would be advantageous? I gleaned from the other answers initializing with "" helps avoiding NPE, but also can result in uncaught bugs.
2

Best practice is to make as many fields final as possible or use default values to avoid clients instantiating bad or incomplete objects. If it makes sense for the field to be an empty string by default (i.e. it will not risk logic errors) then by all means do it that way. Otherwise, at least null will throw an exception when a client invokes a method on an object that hasn't been properly instantiated.

So in general, try to force client code to create complete objects during construction. If you don't know what the string should be at construction time, then have them pass it in as a parameter. If the string is just used for internal logic, it may be better as a local variable or parameter - and in that case I generally prefer null to "" because == null is more explicit than isEmpty().

Comments

1

It all depends on what you want your program to do. Sometimes it can be useful to see whether a string has been set already or not at which null would be best. Other times you want to prevent NullPointers which makes "" better.

Comments

0

I prefer to initialize a String in Java as "", purely because I try to avoid null pointers. Null pointers add a lot of overhead that can be removed by assigning "" instead of null.

2 Comments

String initialization to "" might prove to be a recipe for making bugs harder to find later on. It all depends on the underlying context.
If you get a NPE because you chose null as the default value of a String, this means that you forgot to handle the default case in your code, or that you handled it incorrectly. Setting it to an empty string instead will just hide the bug, or make it appear later, where it will be more difficult to diagnose, IMO.
0

It's really depends what you're doing in your program.

"" (empty string) is connected only with String and it's safer than null. When you call methods from String class for example: replaceAll(), indexOf() the program will work correctly but when you have null, you have NullPointerException all time when you're calling method on it.

Consider if string empty value in your application is not something bad to you, then choose str="".

Notice that very often is better when NullPointerException is thrown because we know about an invalid state in our application, so when you want know about this set String to null by default.

2 Comments

String initialization to "" might prove to be a recipe for making bugs harder to find later on. It all depends on the underlying context.
I don't think it's rule. It really depends on situation and notice that we have StringUtils.isEmpty which return true for "" and null
0

Most often I would say: don't initialize your local variable at all when declaring it, if it would be a dummy value like "" or null. Put the real value in, or wait until you can put it in.

The compiler then will make sure that there is at least one assignment before any possible use of the variable. Only for the seldom cases where the compiler isn't smart enough to figure this out (since it needs information not available to it locally) I would use some initialization, and then usually null is better since it shows with a NullPointerException when I was wrong. (I might add an assert s != null after the point where I think the Initialization should be done to enforce this.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.