String initialization

J

Jack

Which of the following ways is better to initialize a string?

String str = null;

and

String str = "";

The first way may cause NullPointerException.

Thanks.
 
A

Arved Sandstrom

Jack said:
Which of the following ways is better to initialize a string?

String str = null;

and

String str = "";

The first way may cause NullPointerException.

Thanks.

The first way won't cause an NPE any more than the second one will,
since in both cases it'll be a logic error that really causes the
exception. IOW, a logic error with the first case may cause you to
invoke a String method before you assign an object reference to "str",
but in the second case you might set the variable to null before
invoking a method.

The two cases are different. The first case comes about typically
because you need to initialize the String variable (it's a local
variable, say) but you have not yet got a reasonable default to assign
to it (often because of scopes). It's often a mistake to assign it a
default of "" because (1) that probably isn't a reasonable value, and
(2) you've now set yourself up for a whole class of logical errors. With
respect to the latter point, what I mean by that is, now invoking a
String method will succeed, because you've got the value of "", but
you're scratching your head wondering why the results are so whacked,
because you're sure you didn't "really" initialize the variable yet.

This is anecdotal, but I personally almost never have cause to use case #2.

AHS
 
D

Daniel Pitts

Jack said:
Which of the following ways is better to initialize a string?

String str = null;

and

String str = "";

The first way may cause NullPointerException.
No, accessing that string without checking for null WILL cause a
NullPointerException, however just assigning null will never cause a
NullPointerException.

If you want a blanket rule, the rule is It Depends.

"" and null are semantically different, and a lot of problems are
created by trying to consider them the same. null means no value, where
"" is a blank String.

If it makes sense in your API contract that you have a non-empty String,
or a blank String, then the second form makes sense. However, if your
contract needs a way to distinguish between a blank value, and a missing
value, then the first method makes sense.

In general, NullPointerExceptions are an indication that the programmer
incorrectly expected something to be true (an object to be initialized
to a meaningful value). Initializing to "" will only mask the error,
not correct it.
 
R

Roedy Green


quoting from that entry:

There are three ways you might “initialise” a reference String
variable:

1. No initialisation at all: advantage: compiler catches you if you
fail to provide a proper value later (for local variables).

2. null: advantage: fast comparison test. Run time catches you if you
fail to provide a proper value later. Happens automatically for static
and instance variables.

3. "": advantage: The value will be processed just like any other
String without incident. This may be quite suitable of an empty String
is a reasonable default.
 
T

Tom Anderson

Which of the following ways is better to initialize a string?

String str = null;

and

String str = "";

The first way may cause NullPointerException.

You should do:

String str = "FileNotFound";

HTH. HAND.

tom
 
J

Joshua Cranmer

Tom said:
You should do:

String str = "FileNotFound";

There's a running gag on The Daily WTF originating from this enum:

enum TruthValue { FALSE, TRUE, FILE_NOT_FOUND; }
 
T

Tom Anderson

There's a running gag on The Daily WTF originating from this enum:

enum TruthValue { FALSE, TRUE, FILE_NOT_FOUND; }

Joshua, what on earth is the point of having in-jokes if you go around
explaining them to all and sundry?!

tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,610
Members
45,254
Latest member
Top Crypto TwitterChannel

Latest Threads

Top