-2

What is the best practice to use string variable in java? Does it have to be declared and initialized before use/manipulating it.

eg:

String thisString; // is this best practice

String thatString =""; // or is this best practice
vapurrmaid
  • 2,287
  • 2
  • 14
  • 30
Gold Win
  • 7
  • 1
  • 7
  • 2
    Read this: https://stackoverflow.com/questions/465627/use-of-the-stringstring-constructor-in-java – Lukas Bradley Dec 11 '17 at 18:56
  • This depends on many things. Lets say that you have class with only one constructor which expects string parameter and assigns passed argument to string field. In such case assigning to it default value makes no sense because it will always end up with value passed by constructor. As it stays now your question seems too broad. – Pshemo Dec 11 '17 at 19:07
  • "Does it have to be declared... before use..." Yes. Try using a variable, String or otherwise, that you haven't declared and see what happens. – MarsAtomic Dec 11 '17 at 19:13

3 Answers3

1

Afaik there is no strong convention here. I would prefer to initialize a string variable on declaration if I already knew the assignment, like:

String name = generateName();

If it's a static value it should be declared as final static variable:

private final static DEFAULT_NAME = "John Doe";

If you need the variable before the assignment:

String name;
if(useLongNames) {
    name = firstame;
}
else {
    name = title + " " + fistname + " " + lastname;
}
// some code which uses name

In addition you could think about making all varible declarations final which you want only assign once. This would prevent you from some kind of errors which could then already be detected by the IDE.

snap
  • 1,598
  • 1
  • 14
  • 21
1

If it's a field, you'd typically declare it without initialization, and assign it a value in the constructor.

If it's a local variable, best practice is to declare it where you know its intended value, so you can initialize it with a meaningful value:

String myString = "Result = " + xyz;

Introducing a variable not having a meaningful value is (in most cases) bad style. (Why would you intrduce a name (= variable) for something that you haven't yet decided upon?)

In situations like

String myString;
if (someCondition) {
    myString = "A";
} else {
    myString = "B";
}
System.out.println(myString);

where it's hard to use the same statement for declaring and assigning the string, it's best to declare the variable without initialization. Then the compiler checks that in all execution pathes you assign a value to the variable before you use it and never end up with some forgotten assignment.

Ralf Kleberhoff
  • 6,990
  • 1
  • 13
  • 7
0
  1. It all depends on where are you creating string variables.
  2. If you are creating string variable as in instance variable of class then
    you don't need to initialize it, because java will provide default value to that variable i.e null. So you can use String thisString; if it is an instance variable.
  3. If you are going to create string variable inside a function it would be always better to create a variable with null as value. Because null means i haven't initialized this variable and it does not have value and empty string means i know the value and it's empty.
Rohit
  • 146
  • 1
  • 5
  • 1
    I disagree with point 3. Not initializing the variable at all makes the compiler check for execution pathes where you use the string and didn't assign it a value. That's a helpful check, but only works if you leave the variable uninitialized. – Ralf Kleberhoff Dec 11 '17 at 19:09
  • Just to pile on with @RalfKleberhoff's point: if you were meant to assign `null` to the variable, the whole of [JLS Chapter 16](https://docs.oracle.com/javase/specs/jls/se9/html/jls-16.html) would be redundant. – Andy Turner Dec 11 '17 at 19:18
  • Hello Ralf and Andy...thanks your insights on point 3. Its really helpful. I think i took a right decision of coming on to stack overflow to answer questions which helps to clear out my concepts. If we initialize it to empty string will it not occupy certain amount memory ? I know its not that much amount of it, but still it takes up some memory right ? Also if java itself states that default value of string is null, shouldn't we use the same value when we are initializing it inside function ? – Rohit Dec 12 '17 at 04:31