0

I need to know the difference of initializing the String in java when using the runtime.

For Exmaple:

String declare = null;

otherwise:

String declare = "";

I declared the two type of declaration of string. Which one is best for runtime declaration.

2
  • 1
    It depends on what you will be doing with it. Commented Jun 28, 2012 at 11:26
  • Depends if null and "" mean the same or different things to the using code. Commented Jun 28, 2012 at 11:28

4 Answers 4

3

A String is an object. If you initialize it to null, you are telling the compiler that you are aware that wasn't initialized, and that there should be no warnings when you first try to use the variable. Aside from that, you are pointing a reference to null, of course.

If you initialize the String to an empty String, however, the following happens:

  • There's now a String object allocated
  • The compiler will put that String literal in the String pool
  • Any other String that you initialize to "" will point to the same inmutable String from that pool

So, the question is, how do you handle nulls or empty Strings in your code? That's what should guide your decision

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

3 Comments

Now i ask in my code the string refrence named 'declare' occupies the memory or not. That is my main doubt here.
Both use up the space of a pointer. The second approach additionally takes the space for the String "" once (and reuses it for every future declaration)
All options (including null) use the space of a pointer. (That said, it's really bad practice to use null just as a substitute for "".)
0

In first case you have created a 'pointer' to a null object (objec is not created). In second one - the 'pointer' to the object with value "" (empty string). These are qiute different things - you need to decide, which one do you need for further manipulations.

1 Comment

i have a doubt to declare both. Which one occupies memory and which one doesn't. It's the major question for me.
0

First example will not create a String object, and second will. So, the statement:

declare.equals("some string");

will generate a NullPointerException for your first example, but not for the second.

Comments

0

As @AljoshaBre commented, it depends on what you are going to do with it. Having it initialized to null is somewhat redundant, for the initial value usually is that. The blank initialization ("") makes it impossible to receive a null pointer exception if you go through an unexpected path (which may be good or bad, because it might mask logic errors in your code).

Having an initial value is usually good, but make it so that it is meaningful for the code that is going to use your String, not a random initial value.

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.