StringBuilder s = new StringBuilder("Hello
Mom");s.append(",I mean,Mother");
Response.Write(s.ToString());
String s = "Hello Mom";
s+ = ",I mean Mom";
Response.Write(s);
Which is faster ? which uses the most memory?
Answer Posted / jimmy dean
The second one is faster.
In the first example the StringBuilder is initialized with
an initial value. Then the append method is ran, adding
another string to 's'. Then the Write method is called,
which calls the ToString() method of 's'.
Total Steps:
1.) Initialize and set initial value
2.) Append
3.) Write
4.) ToString()
In the second example the String is initialized with an
initial value. Then a string is added onto 's'. Then the
Write method is called.
Total Steps:
1.) Initialize and set initial value
2.) Add String to String
3.) Write
Therefore I would say that the second example is faster, and
uses less memory.
Is This Answer Correct ? | 0 Yes | 3 No |
Post New Answer View All Answers
Can private class be extended java?
What is the purpose of void?
What is tree in java?
What does percent mean in java?
Which method cannot be overridden in java?
java program with complete 4 oops concepts implemented example
Explain what access modifiers can be used for methods?
How does predicate work in java?
What is a static class in java?
What are packages in java?
Why synchronization is important in java?
What is array command?
What is a stringbuilder?
What are the Class Libraries ?
What are the different ways of creating thread?