Reputation: 856
Which one is correct and why:
String dynamic = new String();
dynamic = " where id='" + unitId + "'";
Or
String dynamic = " where id='" + unitId + "'";
Please suggest what is the difference in above 2 initilizaton of String.
Upvotes: 1
Views: 3447
Reputation: 388
String str = new String(); // as there is new operator so you are creating an object of string. This is going to create extra space in memory. So wastage of memory and redundant.Its not advisable to use it until you are not creating some complex object.
But String str ="qwerty" means that u are assigning it to str. As bot are stored in common pool. So its better to use this notation.
Upvotes: 1
Reputation: 69
As we know that String class is immutable so ,
String str="Hello String";
is the best way of using String class so we can avoid memory wastage.When string with the same value created.
Upvotes: 1
Reputation: 121998
String dynamic = new String(); //created an String object
dynamic = " where id='" + unitId + "'"; //overriding reference with new value
Since string is immutable and now dynamic
will store in common pool.
And
String dynamic = " where id='" + unitId + "'";
Is again a literal and stores in common pool.
So , String
can be created by directly assigning a String
literal which is shared in a common pool.
It is uncommon and not recommended to use the new operator to construct a String
object in the heap.
So the line
String dynamic = new String();
Is redundant and allocating unnecessary memory in heap.
Don't do that.
Learn what happens in both the cases, then you come to know.
And finally, do not use much concatenation's with "+", cannot do much harm in lesser amount of concatenations. If you are dealing with larger amount prefer to use StringBuilder
with append method.
Upvotes: 5
Reputation: 3613
I guess the below will be good
String str; // Just declares the variable and the default will be null. This can be done in global scope or outer scope
str = "where id" + unitID + "'"; // Initializing string with value when needed. This will be done in the inner scope.
If declaration and initialization done in a line where the initialization contains dynamic text (unitID in your case) you can't do it globally. If Scope of the variable is not an issue then u may go ahead. Cheers!!
Upvotes: 0
Reputation: 68847
Short answer:
String str = new String();
creates a new String object on the heap. When you do afterwards:
str = "Hello, String Pool";
You simply overwrite the reference to the first object with another reference. Thus, the first object is lost. Keep this in mind: Strings are immutable.
Upvotes: 2
Reputation: 26094
You can use like this.
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("where id='");
stringBuilder.append(unitId);
stringBuilder.append("'");
String dynamic = stringBuilder.toString();
Upvotes: 0
Reputation: 5612
String dynamic = new String();
dynamic = " where id='" + unitId + "'";
creates a binding to an empty string, and then dumps it completely by re-initializing it.
String dynamic = " where id='" + unitId + "'";
creates the binding and leaves it.
The first one is unnecessary. Just go with the direct string literal.
Upvotes: 0
Reputation: 13844
I would suggest you to do in this way
String dynamic = " where id='" + unitId + "'";
String dynamic = new String();// DON'T DO THIS!
dynamic = " where id='" + unitId + "'";
The statement creates a new String instance each time it is executed, and none of those object creations is necessary. The argument to the String constructor (" where id='" + unitId + "'") is itself a String instance, functionally identical to all of the objects created by the constructor. If this usage occurs in a loop or in a frequently invoked method, millions of String instances can be created needlessly. The improved version is simply the following:
String dynamic = " where id='" + unitId + "'";
Upvotes: 0