2

Is it correct to initialize a String as

String value = new String("test");

value of string is assigned in multiple places and if value is null, then default value which is test should be taken, which means if I declare

String value = null;

at some point I have assign a value if in code no value is assigned.

8
  • 3
    It's correct, but is a waste of resources. Just use String value = "test"; I don't understand what you mean with the second part of the question. Commented Jul 26, 2015 at 12:26
  • I don't understood the second part of your question. Commented Jul 26, 2015 at 12:27
  • Are you trying to dodge a NullPointer ? Commented Jul 26, 2015 at 12:27
  • It is better to check for a null instead. if(str != null && your conditions) Commented Jul 26, 2015 at 12:30
  • 2
    are you asking if value is assigned null, then you want to change it to "test" by default? Commented Jul 26, 2015 at 12:31

3 Answers 3

5

I think you won't be able to change value = null to value= "test" by default. If the string "test" is really important to you, when you are accessing value, do this:

  if(value == null){
      value = "test";
  }

Instead of writing this condition everywhere in the code, what you can do is call a function getStringValue() instead of using value.

 String getStringValue(){
    if(value == null){ 
       value = "test"
    }
    return value;
 }

This is same as checking the condition as mentioned above but this produces cleaner code and you don't need to write that condition every time.

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

1 Comment

Thanks Karthik for the insight.
3

Variables can't have a default value that is used if you assign them later to null. That doesn't exist.

If you do

String a = "test"; 
// ...
a = null;

then a will have the value null. If you want to use "test" instead of null, then you have to do it explicitely:

String actualValue = a;
if (actualValue == null) {
    actualValue = "test";
}

or simply

String actualValue = a == null ? "test" : a;

7 Comments

My intention is to avoid using if condition to check string value is null or not by assigning a default value.
But there is no such thing as a default value for a variable. That's my point. If you assign "test" to a a variable, and some code reassigns it to null later, it will become null.
That is fine, at declaration time string is test and if no values are assigned then test value will be taken correct?
Well, yes, if you assign the value "test" to a variable, its value will be "test". That's what an assignment does.
So String value = "test" is declared and test will be it's value if no other assignment is done to variable during execution of the code correct?
|
1

If you want a string not to be null, you can simply check the value before assigning it.

String value = valueCommingFromSomewhere;
if (value == null) {
    value = "myDefaultValue";
}

1 Comment

Thanks for the insight.

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.